8

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?

  • did you find a solution for this? I am trying to sort items in minicart based on their price. – Korbin Jul 27 '18 at 7:37
  • @k185 have you found any clue from my answer below – Amrit Pal Singh Jul 30 '18 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 '18 at 12:14
4
+25

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

Create This Plugin

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

In Your Cart.php

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;
}
  • 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 '18 at 6:42
  • @k185 you can give any other name also for cart.php. I have just created a after plugin for getItems() method. – Rizwan Khan Aug 3 '18 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 – Rizwan Khan Aug 3 '18 at 14:30
  • 1
    @k185 does this works for you? – Rizwan Khan Aug 6 '18 at 16:22
  • that fixed the issue – Korbin Aug 8 '18 at 10:48
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.