1

I tried,

in catalog_category_view.xml file

  <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>

enter image description here

0

2 Answers 2

2

There is a simple reason: Mini cart: get array of last added items. The last item will be pushed at the top.

\Magento\Checkout\CustomerData\Cart::getRecentItems()

 foreach (array_reverse($this->getAllQuoteItems()) as $item) {
   ...... 
 }

We can write a Plugin to re-order the item in Mini cart.

Vendor/MiniCart/frontend/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\CustomerData\Cart">
        <plugin name="item_cart_data" type="Vendor\MiniCart\Plugin\CustomerData\Cart"/>
    </type>
</config>

app/code/Vendor/MiniCart/Plugin/CustomerData/Cart.php

/**
 * @param DataCart $subject
 * @param $result
 * @return mixed $result
 */
public function afterGetSectionData(DataCart $subject, $result)
{
    //Re-order the cart items
    $items = array_reverse($result['items']);
    $result['items'] = $items;
    return $result;
}
0
1

Step 1. Create di.xml under YourModuleName/MiniCart/etc/frontend/ folder

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\CustomerData\Cart">
    <plugin name="extra_data" type="YourModuleName\MiniCart\Plugin\Checkout\CustomerData\Cart"/>
</type>
</config>

Step 2. Create Cart.php under YourModuleName/MiniCart/Plugin/Checkout/CustomerData/ folder

<?php
namespace YourModuleName\MiniCart\Plugin\Checkout\CustomerData;

class Cart {
public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, array $result)
{        
    //Re-order the cart items
    $items = array_reverse($result['items']);
    $result['items'] = $items;
    return $result;
}
}

Step 3. Clear Cache and flush Cache

Hope it'll help you.

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.