Question

When I want to sort products by price (low to high and high to low), my link is not working. Option value is "price&product_list_dir=asc", which encode in "price%26product_list_dir%3Dasc". How can I decode that url?

Here is my code:

<div class="toolbar-sorter sorter">
<label class="sorter-label" for="sorter"><?php /* @escapeNotVerified */ echo __('Sort by') ?></label>
<select id="sorter" data-role="sorter" class="sorter-options selectpicker">
    <?php
        $url = $_SERVER['REQUEST_URI'];
        $path = strstr($url, '?');
    ?>
    <option
        <?php if($path == '?product_list_order=price&product_list_dir=desc'): ?>
            selected="selected"
        <?php endif; ?>
        class="high-low" value="price&product_list_dir=desc">
        Price - High to low
    </option>
    <option
        <?php if($path == '?product_list_order=price&product_list_dir=asc'): ?>
            selected="selected"
        <?php endif; ?>
        class="low-high" value="<?php /* @escapeNotVerified */ echo __('price&product_list_dir=asc') ?>">
        Price - Low to high
    </option>
</select></div>
Was it helpful?

Solution

It's the browser that's encoding the value correctly as expected. Check out https://perishablepress.com/stop-using-unsafe-characters-in-urls/ for safe characters.

In my opinion the best way to do it would be just to set the value to something like "price$product_list_dir!asc" or "price+dir-desc" or just "price_desc" and parse the value of product_list_order in your code. Other options are:

  • Split the menu to two drop-down menus (product_list_order and product_list_dir)
  • Update the url with javascript when the pulldown selection changes

OTHER TIPS

change this echo __('price&product_list_dir=asc') to this echo 'price&product_list_dir=asc'

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top