1

Building on from this question How can I filter Craft Commerce products by price

I have a price filter in the store

Using this code works fine for the normal price

{% set queryParams = {
        relatedTo: category,
        defaultPrice: [
            'and',
            '>= ' ~ 0,
            '<= ' ~ 90,
        ]
    }
%}

but defaultPrice does not consider sale prices.

So if a product is normally $99 but it is reduced to $89, it won't be returned

I would have expected defaultPrice to store the current price being charged. So the sale price if it was on sale.

Is it possible to filter by price and include sale prices?

I've spent hours now on something I thought would be simple. It seems the only way to get this information is to get the products and loop through them. Checking for sale prices.

Do I have to create a plugin to make a usable price filter?

1

https://craftcommerce.com/docs/variant-model#saleprice

The comment on the documentation leads me to believe you can just use salePrice for your filtering as it will be equal to the defaultPrice if there is no sale.

So:

{% set queryParams = {
        relatedTo: category,
        salePrice: [
            'and',
            '>= ' ~ 0,
            '<= ' ~ 90,
        ]
    }
%}
| improve this answer | |
  • I tried this initially too, but that is part of the variant model so not accessible using craft.commerce.products – MalTek Jul 6 '17 at 13:52
  • defaultVariant is, but does not seem to be accessible either. {% set queryParams = { relatedTo: category, defaultVariant: { salePrice: [ 'and', '>= ' ~ 0, '<= ' ~ 90, ] } } %} Does not work either – MalTek Jul 6 '17 at 13:54

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.