2

Currently using Craft Commerce 2.

I was wondering if it's possible to order products by the lowest priced variant. I have tried something along the lines of this, but it doesn't do anything different with the ordering:

{% set products = craft.products.type('accommodation').all() %}

{% for product in products %}
   {% if product.getVariants()|length %}

        {% set variants = craft.variants ({
            product: product,
            orderBy: 'price asc'
        }) %}

        {% for purchasable in variants %}
            // Products Here
        {% endfor %}

    {% endif %}
{% endfor %}

I have also tried something else along the lines of this:

{% set products = craft.commerce.products({
    relatedTo: 'accommodation', 
    order: 'defaultPrice asc',
}) %}
2

I managed to resolve this using the supersort plugin and using 'defaultPrice' instead of 'price'. Example below:

{% set products = craft.products.type('accommodation').all() %}
{% set order = products|supersort('sortAs', '{{ object.defaultPrice }}') %}

{% for product in order %}

    // Products

{% endfor %}

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.