I am facing category page url issue when try to sort products by Price (low to high) or (high to low). I'm using below code to add Price options (high to low).

<option value="<?= /* @escapeNotVerified */ $_key.'&product_list_dir=asc'; ?>"<?php if($block->isOrderCurrent('price') && $block->getCurrentDirection() == 'asc'): ?> selected="selected"<?php endif; ?>>
                <?php echo $block->escapeHtml(__("Price - Low to High")) ?>

                <option value="<?= /* @escapeNotVerified */ $_key.'&product_list_dir=desc'; ?>"<?php if($block->isOrderCurrent('price') && $block->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
                <?php echo $block->escapeHtml(__("Price - High to Low")) ?>

                    <?php else:?>
                    <option value="<?= /* @escapeNotVerified */ $_key ?>"
                <?php if ($block->isOrderCurrent($_key)): ?>
                <?php endif; ?>
                <?= $block->escapeHtml(__($_order)) ?>
        <?php endif; ?>

How to fix this issue.Need help... Category page URL


I had a similar requirement where I need to add some custom options.
You can try following code and modify it according to your need.



use Magento\Framework\Component\ComponentRegistrar;



<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Anshu_PriceSort">
            <module name="Magento_Catalog" />


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="anshu_pricesort_block_product_productlist_toolbar" type="Anshu\PriceSort\Plugin\Catalog\Block\Toolbar" />
    <type name="Magento\Catalog\Model\Config">
        <plugin name="anshu_pricesort_model_config" type="Anshu\PriceSort\Plugin\Catalog\Model\Config" />



namespace Anshu\PriceSort\Plugin\Catalog\Model;

use Magento\Catalog\Model\Config as CatalogConfig;

class Config
    public function afterGetAttributeUsedForSortByArray(CatalogConfig $subject, $result)
        $result['low_to_high'] = __('Price - Low To High');
        $result['high_to_low'] = __('Price - High To Low');
        return $result;



namespace Anshu\PriceSort\Plugin\Catalog\Block;

use Magento\Catalog\Block\Product\ProductList\Toolbar as ProductListToolbar;

class Toolbar
    public function afterSetCollection(ProductListToolbar $subject, $result, $collection)
        switch ($subject->getCurrentOrder()) {
            case 'low_to_high':
                return $result->getCollection()->setOrder('price', 'asc');
            case 'high_to_low':
                return $result->getCollection()->setOrder('price', 'desc');
                return $result;

Here Anshu is the namespace or vendor name and PriceSort is module name.

  • I tried your previous "Anshu_Sortby" module code but not working.Does this work? – Shoaib Saleem Feb 26 at 11:26
  • @ShoaibSaleem For me both the codes are working. You are try this one. This is the better code than the previous one. – Anshu Mishra Feb 26 at 11:40
  • not working in 2.2.6 version. – Shoaib Saleem Feb 27 at 9:56
  • setup namespace command error when try to upgrade setup. – Shoaib Saleem Feb 27 at 9:58
  • @ShoaibSaleem Check if you have given proper permissions. – Anshu Mishra Feb 27 at 10:14

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.