Search code examples
shopifyliquidshopify-api

How to set my price_range filers default lowers value to the CHEAPEST product's price from the collection?


I created a double input field like this: enter image description here

As you can see, the max price value is set to the most expensive product in the collection, however, my minimal value is set to 0... Is there a way to retreat the cheapest price from the collection?

Thank you for any potential help.


Solution

  • we can find a solution using custom code. Here a snippet which I used:

    var minFilterPrice = {{ filterMinPrice | divided_by: 100 | escape }};
    var maxFilterPrice = {{ filterMaxPrice | divided_by: 100 | escape }};
    {% liquid assign filterMinPrice = 0 %}
    {% liquid assign filterMaxPrice = 0 %}
    {% for colProduct in collection.all_products %}
        {% liquid assign current_variant = colProduct.selected_or_first_available_variant %}
        {% liquid assign price = current_variant.price %}
        {% if filterMinPrice > price or filterMinPrice == 0 %} {% liquid assign filterMinPrice = price %} {% endif %}
        {% if filterMaxPrice < price or filterMaxPrice==0 %} {% liquid assign filterMaxPrice=price %} {% endif %} 
    {% endfor %}