0

How to change the product list 'desc' direction?

Now Old product is default.

I want to change is new product position is default > ?product_list_dir=desc

using Magento Version 2.2.4

thank you!

2
  • plz explain properly May 5, 2018 at 15:31
  • want to change the date position : asc to desc <-- @MuhammadAhsanHorani
    – niceonelee
    May 7, 2018 at 17:27

3 Answers 3

1

You should add the below code app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_category_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="product_list_toolbar">
                <action method="setDefaultDirection">
                    <argument name="dir" xsi:type="string">desc</argument>
                </action>
            </referenceBlock>
        </referenceContainer>
    </body>
</page>
0

The only solution I found is this, edit the file located in:

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

add this line:

$this->_direction = strtolower('desc'); //choose 'asc' or 'desc'

in this position:

    public function setCollection($collection)
{
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    $this->_direction = strtolower('desc'); // *** THIS POSITION ***

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    if ($this->getCurrentOrder()) {
        if ($this->getCurrentOrder() == 'position') {
            $this->_collection->addAttributeToSort(
                $this->getCurrentOrder(),
                $this->getCurrentDirection()
            )->addAttributeToSort('entity_id', $this->getCurrentDirection());
        } else {
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }
    }
    return $this;
}

I hope I was helpful

0

Another solution could be overwriting the method prepareSortableFieldsByCategory of the class Magento\Catalog\Block\Product\ListProduct and add the following code to the method

if (!$this->getDefaultDirection()) {
    $this->setDefaultDirection('desc');
}

Complete code:

    public function prepareSortableFieldsByCategory($category)
{
    if (!$this->getAvailableOrders()) {
        $this->setAvailableOrders($category->getAvailableSortByOptions());
    }
    $availableOrders = $this->getAvailableOrders();
    if (!$this->getSortBy()) {
        $categorySortBy = $this->getDefaultSortBy() ?: $category->getDefaultSortBy();
        if ($categorySortBy) {
            if (!$availableOrders) {
                $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
            }
            if (isset($availableOrders[$categorySortBy])) {
                $this->setSortBy($categorySortBy);
            }
        }
    }
    /***Change direction***/
    if (!$this->getDefaultDirection()) {
        $this->setDefaultDirection('desc');
    }

    return $this;
}

Another solution could be using xml mentioned in the following post

Link: Change product sorting from desc to asc on category page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.