<p>Your product element criteria is using a condition param: <code>hasVariants</code>, but it should be <code>hasVariant</code> as per the docs: <a href="https://craftcommerce.com/docs/craft-commerce-products#hasvariant" rel="nofollow noreferrer">https://craftcommerce.com/docs/craft-commerce-products#hasvariant</a></p>

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

CC BY-SA 3.0
2017-03-14T08:35:24.087Z
My bad. Sorry for wasting time on this. Now on to the random results I get from using this correct param: http://craftcms.stackexchange.com/questions/18663/commerce-products-returning-different-results-on-page-load
Clive Portman
CC BY-SA 3.0
2017-03-15T08:13:07.090Z
That secondary issue was fixed in Commerce 1.2.1334
Luke Holder
CC BY-SA 3.0
2017-03-14T03:54:47.903Z
2017-03-14T03:54:47.903Z
Luke Holder
<p>I'm trying to query for certain products but only ones that have stock (the products just have defaultVariants):</p>

<pre><code>{% 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, &gt; ' ~ earliestDate ~ ', &lt; ' ~ latestDate,
    order: 'date',
    limit: 5
}) %}
</code></pre>

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

<pre><code>{% if product.defaultVariant.stock &gt; 0 %}
    {{ product.title }}
{% endif %}
</code></pre>

<p>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.</p>

<p>Running Craft Commerce 1.2.1337 so am up-to-date.</p>

<p>Could be related to: <a href="https://craftcms.stackexchange.com/questions/18663/commerce-products-returning-different-results-on-page-load">Commerce products returning different results on page load</a></p>

<p>Someone else mentioning it in comment to answer of this: <a href="https://craftcms.stackexchange.com/questions/17160/how-do-i-check-if-a-product-has-stock-in-any-of-its-variants/17168#17168">How do I check if a product has stock in any of it&#39;s variants?</a></p>

CC BY-SA 3.0
2017-03-11T00:59:25.563Z
Clive, looking at both of your SO questions, what is the `date` param in your query? did you mean `postDate` ?
Luke Holder
CC BY-SA 3.0
2017-03-12T14:05:24.720Z
Hi. No, `date` is a custom field.
Clive Portman
CC BY-SA 3.0
2017-03-13T15:14:30.823Z
Please send an email to support@craftcommerce.com, and include a copy of your DB backup. Also include your query twig code, and the expected product IDs that should show up. Thanks.
Luke Holder
<plugin-craftcommerce><stock><products>
Craft Commerce hasStock not working?
# Craft Commerce hasStock not working?

(Asked by: username_0 on 2017-03-10)

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?

**Tags:** plugin-craftcommerce, stock, products

> **Comments:**
> **username_1**: Clive, looking at both of your SO questions, what is the `date` param in your query? did you mean `postDate` ?  
> **username_0**: Hi. No, `date` is a custom field.  
> **username_1**: Please send an email to support@craftcommerce.com, and include a copy of your DB backup. Also include your query twig code, and the expected product IDs that should show up. Thanks.  

---

## 1 answers

### Answer by username_1 on 2017-03-14. Score: 4 | Accepted answer

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.

> **Comments:**
> **username_0**: My bad. Sorry for wasting time on this. Now on to the random results I get from using this correct param: http://craftcms.stackexchange.com/questions/18663/commerce-products-returning-different-results-on-page-load  
> **username_1**: That secondary issue was fixed in Commerce 1.2.1334  

---

