Answers.Body: <p>Jeremy's answer is right, but for people using the Digital Products plugin, this command might be a better fit:</p>
  <pre><code>{# Has the customer explicitly bought the XXX product? Returns 0 (==false) or &gt;= 1 (==true) #}
  {% set licenses = craft.digitalProducts
      .licenses
      .owner(currentUser)
      .product(productID)
      .count()
  %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-13T15:03:58.823Z
Answers.LastActivityDate: 2021-02-13T15:03:58.823Z
Answers.UserDisplayName: Tim Coysh
Answers.Body: <p>There is a direct way to find this out.</p>
  <p>Here's an example.  Note the <code>id</code> you supply must be the specific purchasableId, not the product id (so the <code>defaultVariant.id</code> or <code>variant.id</code>).</p>
  <pre><code>{# Has the customer explicitly bought the XXX product? Returns 0 (==false) or &gt;= 1 (==true) #}
  
  {% set purchasedXXX = craft.orders()
      .user(currentUser)
      .hasPurchasables(10637)
      .isCompleted()
      .count()
  %}
  </code></pre>
  <p>You can then use that as just</p>
  <pre><code>{% if purchasedXXX %}
   ...
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-16T02:31:36.767Z
Answers.LastActivityDate: 2020-09-17T04:40:14.740Z
Answers.LastEditDate: 2020-09-17T04:40:14.740Z
Answers.UserDisplayName: Jeremy Daalder
Answers.Body: <p>Try something like this:</p>
  
  <pre><code>  {% set found = false %}
    {% set orders = craft.orders()
      .customer(craft.commerce.customer)
      .all() %}
    {% for order in orders %}
      // here you could / should also check if the order was paid
      {% for lineItem in order.lineItems %}
        {% if lineItem.purchasableId == yourProductId %}
          {% set found = true %}
        {% endif %}
      {% endfor %}
    {% endfor %}
    {% if found %}
        // user has bought product before 
    {% endif %}
  </code></pre>
  
  <p>There might be more efficient query-wise.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-16T02:32:34.347Z
Answers.Text: There's a much more direct way available with recent Commerce versions, see my answer.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-30T14:48:02.590Z
Answers.LastActivityDate: 2019-09-30T14:48:02.590Z
Answers.UserDisplayName: Seb
Body: <p>I have a digital product, with a landing page, and I want to know if the connected user has already bought the product so I can give him access to the content.</p>
  
Tags: <current-user><products>
Title: Commerce: How can I check that a user has bought a product?
ThreadText: # Commerce: How can I check that a user has bought a product?
  
  (Asked by: username_0 on 2019-09-29)
  
  I have a digital product, with a landing page, and I want to know if the connected user has already bought the product so I can give him access to the content.
  
  **Tags:** current-user, products
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2021-02-13. Score: 1
  
  username_2's answer is right, but for people using the Digital Products plugin, this command might be a better fit:
  
  ```
  {# Has the customer explicitly bought the XXX product? Returns 0 (==false) or >= 1 (==true) #}
  {% set licenses = craft.digitalProducts
      .licenses
      .owner(currentUser)
      .product(productID)
      .count()
  %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2020-09-16. Score: 1
  
  There is a direct way to find this out.
  
  Here's an example. Note the `id` you supply must be the specific purchasableId, not the product id (so the `defaultVariant.id` or `variant.id`).
  
  ```
  {# Has the customer explicitly bought the XXX product? Returns 0 (==false) or >= 1 (==true) #}
  
  {% set purchasedXXX = craft.orders()
      .user(currentUser)
      .hasPurchasables(10637)
      .isCompleted()
      .count()
  %}
  
  ```
  
  You can then use that as just
  
  ```
  {% if purchasedXXX %}
   ...
  {% endif %}
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2019-09-30. Score: 0
  
  Try something like this:
  
  ```
    {% set found = false %}
    {% set orders = craft.orders()
      .customer(craft.commerce.customer)
      .all() %}
    {% for order in orders %}
      // here you could / should also check if the order was paid
      {% for lineItem in order.lineItems %}
        {% if lineItem.purchasableId == yourProductId %}
          {% set found = true %}
        {% endif %}
      {% endfor %}
    {% endfor %}
    {% if found %}
        // user has bought product before 
    {% endif %}
  
  ```
  
  There might be more efficient query-wise.
  
  > **Comments:**
  > **username_2**: There's a much more direct way available with recent Commerce versions, see my answer.  
  
  ---
  
  