Varshini
Varshini

Reputation: 153

How do I show only certain filtering options for the Shopify collection sorting options rather than showing all the options?

I want my collection sorting options to only show the the following: Best Selling, Price (low to high), Price (high to low), Alphabetically (A-Z), Alphabetically (Z-A)

When I add the following code, it successfully shows only the values which allows users to sort products by their price (high to low/low to high)

{% for option in collection.sort_options %}
  {% if option.value contains 'price-' %}
    <option value="?sort_by={{ option.value }}" {% if option.value == selected %}selected{% endif %}>
      {{ option.name }}
    </option>
  {% endif %}
{% endfor %}

The for loop for the collection sorting options results in:

<select name="sort_by">
  <option value="manual">Featured</option>
  <option value="best-selling">Best selling</option>
  <option value="title-ascending">Alphabetically, A-Z</option>
  <option value="title-descending">Alphabetically, Z-A</option>
  <option value="price-ascending">Price, low to high</option>
  <option value="price-descending">Price, high to low</option>
  <option value="created-ascending">Date, old to new</option>
  <option value="created-descending">Date, new to old</option>
</select>

However, I also want to add values that contain either best or title but when I replace

{% if option.value contains 'price-' %}

with

{% if option.value contains 'price-' or 'best-' %}

it just ends up showing ALL the values rather than just the ones containing price or best. How do I fix this?

Upvotes: 0

Views: 1095

Answers (1)

Vladimir
Vladimir

Reputation: 2559

You need to repeat the whole condition statement after the or operator:

{% if option.value contains 'price-' or option.value contains 'best-' %}
  ...
{% endif %}

Upvotes: 1

Related Questions