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?
date
param in your query? did you meanpostDate
?date
is a custom field.