In want to print a list of orders:

{% set orders = craft.orders.completed(true).limit(null).all() %}

Now I want to make a list of orders with only the product.type XYZ.

I tried to fabricate something out of this question. But I'd like specific product types:

{% set orders = craft.orders.completed(true).hasPurchasables([type.XYZ]).limit(null).all() %}

This returns:

Variable "type" does not exist.

How do I have to write this?


You're close. Products themselves aren't Purchasebles.

However, Variants of products are. And you can have more than one Variant of a Product.

So, we need to get all Variants of a particular Product type and then feed your query:

{# get products of a certain type #}
{% set productIds = craft.products.type('classes').ids() %}

{# get the variants from those product Ids #}
{% set theVariants = craft.variants.productId(productIds).all() %}

{# grab orders #}
{% set orders = craft.orders.completed(true).hasPurchasables(theVariants).limit(null).all() %}

You'll probably want to limit the orders returned at some point depending on how you're displaying or querying them.

  • It works. But I just noticed that if a customer has two items of product.type ordered. Then the order is shown twice.
    – KSPR
    May 26 '20 at 12:17
  • I tried to query the thing with .unique(). No luck so far
    – KSPR
    May 26 '20 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.