Answers.Body: <p>As of Commerce 1.1.1211 you can do:</p>
  
  <pre><code>{% if currentUser %}
  {% set order = craft.orders.user(currentUser).hasPurchasables(product.defaultVariant).all() %}
  {% if order|length %}
      &lt;strong&gt;I already own this product: &lt;/strong&gt; &lt;a href="/shop/customer/order?number={{ order[0].number }}"&gt;Order #{{ order[0].shortNumber }}&lt;/a&gt;
  {% endif %}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T23:26:04.003Z
Answers.LastActivityDate: 2019-10-01T23:26:04.003Z
Answers.UserDisplayName: Luke Holder
Answers.Body: <p>First you'll need to grab the user's orders. Then you can check the product type through the purchasable. </p>
  
  <p>Something like this:</p>
  
  <pre><code>{% set orders = craft.commerce.orders.user(currentUser).find() %}
  
  {% if orders|length %}
      {% for order in orders %}
          {% for lineItem in order.lineItems %}
              {{ lineItem.purchasable.product.typeId }} {# 1 #}
          {% endfor %}
      {% endfor %}
  {% endif %}
  </code></pre>
  
  <p>You can take that a step further by doing something like this:</p>
  
  <pre><code>{% set orders = craft.commerce.orders.user(currentUser).find() %}
  {% set product = false %} {# set the condition to false by default. #}
  
  {% if orders|length %}
      {% for order in orders %}
          {% for lineItem in order.lineItems if lineItem.purchasable.product.typeId == 1 %}
              {% set product = true %}
          {% endfor %}
      {% endfor %}
  {% else %}
      {# No orders found. #}
  {% endif %}
  
  ...
  
  {% if product %}
      {# User has purchased product type at some point in time. #}
  {% else %}
      {# User has never purchased product type. #}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-24T15:54:10.670Z
Answers.LastActivityDate: 2016-09-24T18:15:44.200Z
Answers.LastEditDate: 2016-09-24T18:15:44.200Z
Answers.UserDisplayName: Damon
Body: <p>I am building a plugin that grants access to a "hidden" (for lack of better words) entry. if a user has purchased a specific product <em>type</em>.</p>
  
  <p>For example, I don't care which piece of clothing the user purchases, just as long as it was of type clothing. </p>
  
  <p>How can I check the users order history to see if they have purchased a qualifying product?</p>
  
  <p>Thank you for any suggestions!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-01T23:27:33.203Z
Comments.Text: Could you change your selected answer to mine please.
Comments.UserDisplayName: Luke Holder
Tags: <plugin-development><plugin-craftcommerce>
Title: Commerce: How can I check order history to see if a product type has been purchased?
ThreadText: # Commerce: How can I check order history to see if a product type has been purchased?
  
  (Asked by: username_0 on 2016-09-24)
  
  I am building a plugin that grants access to a "hidden" (for lack of better words) entry. if a user has purchased a specific product *type*.
  
  For example, I don't care which piece of clothing the user purchases, just as long as it was of type clothing.
  
  How can I check the users order history to see if they have purchased a qualifying product?
  
  Thank you for any suggestions!
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  > **Comments:**
  > **username_1**: Could you change your selected answer to mine please.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-10-01. Score: 0 | Accepted answer
  
  As of Commerce 1.1.1211 you can do:
  
  ```
  {% if currentUser %}
  {% set order = craft.orders.user(currentUser).hasPurchasables(product.defaultVariant).all() %}
  {% if order|length %}
      <strong>I already own this product: </strong> <a href="/shop/customer/order?number={{ order[0].number }}">Order #{{ order[0].shortNumber }}</a>
  {% endif %}
  {% endif %}
  
  ```
  
  
  ---
  
  ### Answer by username_0 on 2016-09-24. Score: 4
  
  First you'll need to grab the user's orders. Then you can check the product type through the purchasable.
  
  Something like this:
  
  ```
  {% set orders = craft.commerce.orders.user(currentUser).find() %}
  
  {% if orders|length %}
      {% for order in orders %}
          {% for lineItem in order.lineItems %}
              {{ lineItem.purchasable.product.typeId }} {# 1 #}
          {% endfor %}
      {% endfor %}
  {% endif %}
  
  ```
  
  You can take that a step further by doing something like this:
  
  ```
  {% set orders = craft.commerce.orders.user(currentUser).find() %}
  {% set product = false %} {# set the condition to false by default. #}
  
  {% if orders|length %}
      {% for order in orders %}
          {% for lineItem in order.lineItems if lineItem.purchasable.product.typeId == 1 %}
              {% set product = true %}
          {% endfor %}
      {% endfor %}
  {% else %}
      {# No orders found. #}
  {% endif %}
  
  ...
  
  {% if product %}
      {# User has purchased product type at some point in time. #}
  {% else %}
      {# User has never purchased product type. #}
  {% endif %}
  
  ```
  
  
  ---
  
  