0

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>
  • What is not working? Is the "selected" not echoed properly? Is there extraneous "%26 and %3D" in the value attribute of the html so it says like '<option value="price%26product_list_dir%3Dasc">'? Is the sort order not changing after you select a different type of sort? Please specify what is not working so we can help you more quickly. – jous Jan 22 '19 at 11:37
  • I checked with Inspect Element and option value is echoed properly. When I click on the option, page redirect to mysite.com/category.html?product_list_order=price%26product_list_dir%3Dasc . But when I change "%26" and "%3D" into "&" and "&" directly into Address Bar, it works. It seems that, when I click on the option, Magento encode that option value in url – Bobo Jan 22 '19 at 12:42
0

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
  • Thank you. I added onChange="window.document.location.href=this.options[this.selectedIndex].value;" to select and <script type="text/javascript"> jQuery(function($) { $('select').on('change', function() { var url = $(this).val(); if (url) { window.location = url; } return false; }); }); </script> Now it's working. Thank you – Bobo Jan 23 '19 at 22:48
1

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

  • Thank you for your help but it's not working – Bobo Jan 22 '19 at 8:51

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.