Answers.Body: <p>Use the <code>hasPurchasable</code> order query param:</p>
  
  <p><a href="https://docs.craftcms.com/commerce/v2/dev/element-queries/order-queries.html#haspurchasables" rel="nofollow noreferrer">https://docs.craftcms.com/commerce/v2/dev/element-queries/order-queries.html#haspurchasables</a></p>
  
  <pre><code>{% set myOrderQuery = craft.orders()
      .hasPurchasables(product.defaultVariant)
      .limit(10)
  %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-24T10:28:18.480Z
Answers.Text: Just what I needed, that's working. Thanks Luke
Answers.UserDisplayName: Tim Print
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-22T10:15:13.440Z
Answers.LastActivityDate: 2019-06-23T03:21:53.780Z
Answers.LastEditDate: 2019-06-23T03:21:53.780Z
Answers.UserDisplayName: Luke Holder
Body: <p>I'm trying to show some Commerce 2 order information on the front-end. I can't work out the correct syntax. I'm trying to list orders that include a specific product.</p>
  
  <p>I have orders for item SKU "abc123" but this is returning no results.</p>
  
  <pre><code>{% set myOrderQuery = craft.orders()
      .relatedTo("abc123")
      .limit(10)
      %}
  
  {% set orders = myOrderQuery.all() %}
  
  {% if orders|length %}
    &lt;ul&gt;
      {% for order in orders %}
        {% for item in order.lineItems %}
          &lt;li&gt;{{ item.sku }}&lt;/li&gt;
        {% endfor %}
      {% endfor %}
    &lt;/ul&gt;
  {% else %}
    &lt;p&gt;No results&lt;/p&gt;
  {% endif %}
  </code></pre>
  
  <p>If I remove the .relatedTo parameter I get results including orders for that item. How do I just return orders that include that item?</p>
  
  <p>I've tried various permutations of <code>relatedTo</code> and search but haven't found anything that works yet.</p>
  
Tags: <plugin-craftcommerce><search><relatedto>
Title: Searching for item SKU in Commerce orders
ThreadText: # Searching for item SKU in Commerce orders
  
  (Asked by: username_0 on 2019-06-20)
  
  I'm trying to show some Commerce 2 order information on the front-end. I can't work out the correct syntax. I'm trying to list orders that include a specific product.
  
  I have orders for item SKU "abc123" but this is returning no results.
  
  ```
  {% set myOrderQuery = craft.orders()
      .relatedTo("abc123")
      .limit(10)
      %}
  
  {% set orders = myOrderQuery.all() %}
  
  {% if orders|length %}
    <ul>
      {% for order in orders %}
        {% for item in order.lineItems %}
          <li>{{ item.sku }}</li>
        {% endfor %}
      {% endfor %}
    </ul>
  {% else %}
    <p>No results</p>
  {% endif %}
  
  ```
  
  If I remove the .relatedTo parameter I get results including orders for that item. How do I just return orders that include that item?
  
  I've tried various permutations of `relatedTo` and search but haven't found anything that works yet.
  
  **Tags:** plugin-craftcommerce, search, relatedto
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-06-22. Score: 2 | Accepted answer
  
  Use the `hasPurchasable` order query param:
  
  <https://docs.craftcms.com/commerce/v2/dev/element-queries/order-queries.html#haspurchasables>
  
  ```
  {% set myOrderQuery = craft.orders()
      .hasPurchasables(product.defaultVariant)
      .limit(10)
  %}
  
  ```
  
  > **Comments:**
  > **username_0**: Just what I needed, that's working. Thanks username_1  
  
  ---
  
  