3

In Magento 1.x versions we have the function called getOrderUrl() in Toolbar.php whereas in Magento 2, I couldn't found the same function.

My question here is, How can I get the same function? So that I can add few more sort options in toolbar area.

In Magento 1.x versions, <option value="<?php echo $this->getOrderUrl('price', 'asc') ?>"<?php if($this->isOrderCurrent('price') && $this->getCurrentDirection() == 'asc'): ?> selected=”selected”<?php endif; ?>>Price : Low To High</option>. Here getOrderUrl will be generated in value attribute.

I want to generate the getOrderUrl in Magento 2.

It would be appreciated someone provide solution.

2

The function getOrderUrl() was removed in Magento 2. In Magento 1, this function provided us a real order url. However, in Magento 2, we use Javascript to process the order url: vendor/magento/module-catalog/view/frontend/web/js/product/list/toolbar.js

2
  • Yes you are correct. I have to make some changes according to the requirement. – TS Guhan Oct 12 '16 at 8:56
  • TS Guhan Can you please tell me changes as I also need to price asc and desc in sorting – Ankita Agrawal Oct 17 '16 at 10:44
0

In case someone else wants to change the default direction you can use a plugin like this:

Make a file di.xml inside Vendor/Catalog/etc/frontend

di.xml

<?xml version="1.0"?>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
    <plugin sortOrder="1" name="vendorCatalogToolbar"
            type="Vendor\Catalog\Plugin\Block\Product\ProductList\ToolbarPlugin"/>
</type>

and then make the file ToolbarPlugin.php inside Vendor/Catalog/Plugin/Block/Product/ProductList/ToolbarPlugin.php

The code here is :

namespace Vendor\Catalog\Plugin\Block\Product\ProductList; class ToolbarPlugin {

public function beforeSetDefaultDirection(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject, $dir)
{

    if($subject->getCurrentOrder() == 'name'){
        $dir= 'asc';
    }
    return $dir;
}

}

I m doing it like this because the default direction for my site has been set to desc with the use of xml.

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.