I'm trying to query for certain products but only ones that have stock (the products just have defaultVariants):

{% set earliestDate = now | date_modify('- 1 days') %} 
{% set latestDate = now | date_modify('+ 10 days') %}
{% set products = craft.commerce.products({
    hasVariants: {
        hasStock: true
    type: 'class',
    date: 'and, > ' ~ earliestDate ~ ', < ' ~ latestDate,
    order: 'date',
    limit: 5
}) %}

The above returns 5 products, all of which happen to have no stock left as identified by them not showing when I use:

{% if product.defaultVariant.stock > 0 %}
    {{ product.title }}
{% endif %}

I'm currently getting products to show by setting the limit to 25 and then it only shows 1 product in stock. Meaning the query is returning 25 products, of which 24 have no stock so shouldn't be returned.

Running Craft Commerce 1.2.1337 so am up-to-date.

Could be related to: Commerce products returning different results on page load

Someone else mentioning it in comment to answer of this: How do I check if a product has stock in any of it's variants?

  • Clive, looking at both of your SO questions, what is the date param in your query? did you mean postDate ? Commented Mar 11, 2017 at 0:59
  • Hi. No, date is a custom field. Commented Mar 12, 2017 at 14:05
  • Please send an email to [email protected], and include a copy of your DB backup. Also include your query twig code, and the expected product IDs that should show up. Thanks. Commented Mar 13, 2017 at 15:14

1 Answer 1


Your product element criteria is using a condition param: hasVariants, but it should be hasVariant as per the docs: https://craftcommerce.com/docs/craft-commerce-products#hasvariant

That correctly only returns products (where a variant that belongs to that product) is in stock.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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