1

I have a great many products which have a date field (they're class dates) and I want to show the next three which have any stock left (available places). The following works fine to display the next three products:

{% set earliestDate = now %} 
{% set latestDate = now | date_modify('+ 10 days') %}

{% set products = craft.commerce.products({
  type: 'class',
  date: 'and, > ' ~ earliestDate ~ ', < ' ~ latestDate,
  order: 'date',
  limit: 3
}) %}

But of course there's no reference to stock. If I add the hasStock parameter, like so:

{% set products = craft.commerce.products({
  hasVariant: {
    hasStock: true
  },
  type: 'class',
  date: 'and, > ' ~ earliestDate ~ ', < ' ~ latestDate,
  order: 'date',
  limit: 3
}) %}

The results are different every time I load the page! Results vary but include:

  • showing more than three products, e.g. 5 products
  • no products being returned at all
  • three products but products which are an earlier date being skipped

Has anyone else encountered this? I can understand maybe different results than expected if hasStock items were being fetched before the rest of the query, but not why it would be different on every pageload. If it helps, no products are being returned outside the date range I'm supplying.

  • Is this still an issue? – Luke Holder Jul 11 '17 at 5:06
  • Hi Luke. I got around this by avoiding querying using hasStock. It wasn't ideal but I haven't worked on that project for a while. Next time I do I'll have a look. – Clive Portman Jul 12 '17 at 17:39

Your Answer

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

Browse other questions tagged or ask your own question.