9

I am trying to sort the items based on the product ordered qty.

Is there any logical method to the way cart items are sorted by ordered qty?

3
  • did you find a solution for this? I am trying to sort items in minicart based on their price.
    – Korbin
    Jul 27, 2018 at 7:37
  • @k185 have you found any clue from my answer below Jul 30, 2018 at 8:03
  • @AmritPalSingh I tried to extend the Cart.php but I keep getting 'php fatal error' sandbox.onlinephpfunctions.com/code/…
    – Korbin
    Aug 2, 2018 at 12:14

2 Answers 2

4
+25

You can use getItems() plugin to Sort Quote Items:

Create This Plugin:

MODULE\etc\di.xml

<type name="Magento\Checkout\Block\Cart">
        <plugin name="item_filter" type="[Your]\[Module]\Plugin\Checkout\Block\Cart" />
</type>

MODULE\Plugin\Checkout\Block\Cart:

public function afterGetItems(\Magento\Checkout\Block\Cart $subject){
     $items = [];
     $totalItems = [];
     $itemAdded = [];

     foreach ($subject->getQuote()->getAllItems() as $item) {
        $totalItems[] = $item->getId();

        if(/*Your Condition*/){
            /*you can sort item by qty wise*/
            /*you can get qty using $item->getQty();*/
           $itemAdded[] = $item;
           $items[] = $item;
        }

     }
     $result = array_diff($totalItems, $itemAdded);

     foreach ($result as $quoteid) {
        if($quoteid){
            $item = $this->cart->getQuote()->getItemById($quoteid);
            $items[] = $item;
        }
     }

    return $items;
}
7
  • I am not sure how I should implement the Cart.php, I added the blog after 'public function getItems()' but after compile it shows MiBPHP Fatal error: Cannot declare class Magento\Checkout\Block\Cart, because the name is already in use in /var/www/MYSITE/vendor/MYSITE/MYMODULE/Plugin/Checkout/Block/Cart.php on line 272
    – Korbin
    Aug 3, 2018 at 6:42
  • @k185 you can give any other name also for cart.php. I have just created a after plugin for getItems() method. Aug 3, 2018 at 14:24
  • Or you might have given wrong namespace to that class .Magento\Checkout\Block\Cart, it should be Your\Module\Plugin\Checkout\Block\Cart Aug 3, 2018 at 14:30
  • 1
    @k185 does this works for you? Aug 6, 2018 at 16:22
  • 1
    You only posted half of the code
    – Black
    Nov 30, 2020 at 11:29
1

First create a plugin,

In di.xml use :

<type name="Magento\Checkout\CustomerData\Cart">
     <plugin name="SortByQty" type="Vendor\Module\Plugin\Cart"/>
</type>

Then in Cart.php add a function afterGetSectionData,

Now the logic will be done for sorting but i can tell the starting:

public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result){


        $items = $result['items'];

        foreach ($items as $key => $value) {
            $_productId = $value['product_id'];
            $_productPrice = $value['product_price_value'];
            $_productQty = $value['qty'];
            $_productTotal = $_productPrice * $_productQty;
        }

         $data = [
            'items' => $items
         ];

        return $result = array_merge($data, $result);
}

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.