4

I set default sort by price in admin but still i need to sort price from high to low. Magento by default sort it low to high. i try to find option in admin but can't find that. can any one tell me code for that to make it high to low sort by default? I am using porto theme for my store.

6
+50

Copy the file vendor/magento/module-catalog/view/frontend/layout/catalog_category_view.xml to your theme i.e; app/design/frontend/{{Namespace}}/{{Theme}}/Magento_Catalog/layout/catalog_category_view.xml.

And place the following code into the XML file.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list">
                <action method="setDefaultDirection">
                    <argument name="dir" xsi:type="string">desc</argument>
                </action>
            </referenceBlock>
        </referenceContainer>
    </body>
</page>
| improve this answer | |
3

For invert your result from ASC to Desc at load time. Simply add this code in your extended Magento_Catalog/layout/catalog_category_view.xml

<body>
    ...
    <referenceContainer name="content">
       <referenceBlock class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list">
            <action method="setDefaultDirection">
                <argument name="dir" xsi:type="string">desc</argument>
            </action>
        </referenceBlock>
    </referenceContainer>
    ...
</body>
| improve this answer | |
1

You must have to override the getAvailableOrder() and setCollection() function of the Magento

/vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php

Below is the code which may help you.

public function aroundGetAvailableOrders(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed)
{
    $returnValue = $proceed();
    unset($returnValue['price']);
    $returnValue['priceDesc'] = 'price - high to low';
    $returnValue['priceAsc'] = 'price - low to high';
    return $returnValue;
}

public function aroundSetCollection(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed, $collection)
{
    $returnValue = $proceed($collection);
    if ($subject->getCurrentOrder() == 'priceHighToLow') {
        $collection->addAttributeToSelect('*')->setOrder('price', 'ASC');
        $collection->load();
    }
    return $collection;
}
| improve this answer | |

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.