<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>

CC BY-SA 4.0
2021-02-13T15:03:58.823Z
2021-02-13T15:03:58.823Z
Tim Coysh
<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>

CC BY-SA 4.0
2020-09-16T02:31:36.767Z
2020-09-17T04:40:14.740Z
2020-09-17T04:40:14.740Z
Jeremy Daalder
<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>

CC BY-SA 4.0
2020-09-16T02:32:34.347Z
There's a much more direct way available with recent Commerce versions, see my answer.
Jeremy Daalder
CC BY-SA 4.0
2019-09-30T14:48:02.590Z
2019-09-30T14:48:02.590Z
Seb
<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>

<current-user><products>
Commerce: How can I check that a user has bought a product?
# 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

Jeremy'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.  

---

