In Craft Commerce, how can I show only products that are in stock when not using variants?

For example, this query, but only show products with a stock with 1 or more:

{% set productQuery = craft.products()
        .order('postDate desc')
        .limit(4) %}

    {% set products = productQuery.all() %}

    {% for product in products %}
      {% include "_components/product-blurb.html" %}
    {% endfor %}

You're always using variants. Just that if you have no 'other' variants commerce hides that stuff away. But your product has a defaultVariant behind the scenes.


.hasVariant({ hasStock: true })

…to your query should (I think) return your products that have ([hidden] variants in) stock.

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.