How can I sort the products in the cart in alphabetical order?




foreach($this->getItems() as $_item):


$cartitems = Mage::getModel('sales/quote_item')->getCollection()
->addOrder('name', 'asc');

$items = array();
foreach ($cartitems as $item) {
if (!$item->isDeleted() && !$item->getParentItemId()) {
$items[] = $item;

I think you will need to customize the code a bit.

Or another solution may be :


Update line : $this->_items = Mage::getModel('sales/quote_item')->getCollection();


 $this->_items = Mage::getModel('sales/quote_item')->getCollection()->setOrder('name','asc');

I haven't tried it though.

  • Thank you, I found a similar code to this but can't get it to work. It comes up with an error 'Parse error: syntax error, unexpected T_ENDFOREACH in ..../checkout/cart.phtml on line 143 I presume i'm leaving behind the <?php and ?> before / after it? – user3217495 Jul 15 '14 at 6:33
  • This is what my code looks like which gives me an error message; <tbody> <?php $cartitems = Mage::getModel('sales/quote_item')->getCollection() ->setQuote($this->getQuote()) ->addFieldToSelect('name') ->addOrder('name', 'asc'); $items = array(); foreach ($cartitems as $item) { if (!$item->isDeleted() && !$item->getParentItemId()) { $items[] = $item; } } ?> <?php echo $this->getItemHtml($_item) ?> <?php endforeach ?> </tbody> – user3217495 Jul 15 '14 at 6:36
  • Even if this works, the cart is already slow so I don't think there is a need for an additional query to retrieve the items that are already retrieved by getItems. I think a better approach would be just to use the results of getItems loop through them and order by name. – Marius Jul 15 '14 at 6:43
  • Marius, can you explain a little more about you idea of looping through them and order by name? Sorry, real novice here! – user3217495 Jul 15 '14 at 6:45
  • Updated my answer may be that can help – TBI Infotech Jul 15 '14 at 8:27

Extend class Mage_Checkout_Block_Cart and override method getItems. It returns an array of items that contains objects of Mage_Sales_Model_Quote_Item. You could use $_item->getName() to access the product name. Sort the array of objects.


Quick: Copy app/design/frontend/base/default/checkout/cart.phtml to app/frontend/[mypackage]/[my_current_store_theme]/checkout/cart.phtml In the first lines place your code:

 $myItems = $this->getItems();
 // sort $myItems

Replace bellow in same file:

<?php /* foreach($this->getItems() as $_item): */ ?>
<?php /* with: */ ?>
<?php foreach($myItems as $_item): ?>

Magento way: Create a custom module after reading a tutorial about how to (there are plenty). http://www.magentocommerce.com/magento-connect/create_your_extension/ Here there is a nice documentation that explains in detail how to create an extension: Extension Developer's (including module). http://info.magento.com/rs/magentocommerce/images/Magento-Extension-Developers-Guide-v1.0.pdf

Or use an extension for module creation: http://www.magentocommerce.com/magento-connect/ultimate-module-creator.html I would read first the tutorial if I would be new to Magento.

You'll need to overwrite the class Mage_Checkout_Block_Cart. First declare module's config.xml and rewrite the block class. You're telling Magento to use your class instead of the class from core. Don't forget to declare the module in app/etc/modules/Mynamespace_Mymodule.xml too.

<?xml version="1.0" encoding="UTF-8"?>

        <mymodule> <!-- or <mymodule_something>-->


    <helpers> <!-- You need this too: A declared class Mynamespace_Mymodule_Helper_Data in Helper dir in current module -->
        <mymodule> <!-- or <mymodule_something> to Mage::helper('mymodule_something/data') instead of Mage::helper('mymodule/data') -->

In app/code/local/Mynamespace/Mymodule/Block/Cart.php:

class Mynamespace_Mymodule_Block_Cart extends Mage_Checkout_Block_Cart
    public function getItems()
        $items = parent::getItems();

        /* Here you sort $items in alphabetic order.
           Maybe with usort php function ?
           Or with array_multisort:
        $a = array();
        foreach ($items as $key => $item) {
            $a[$key] = $item->getName();
        array_multisort($a, $items);

        return $items;
  • Where do I change all this, sorry? – user3217495 Jul 14 '14 at 23:44
  • You create a new local module and extend the class block Mage_Checkout_Block_Cart. There are tutorials for this. – obscure Jul 15 '14 at 0:02
  • Still don't get what i'm really doing. But thanks anyway. – user3217495 Jul 15 '14 at 5:34
  • I edit the answer. – obscure Jul 15 '14 at 8:41
  • Here is another extension for products sorting, You can sort product using multiple filters. fmeaddons.com/magento/… – user9468 Jan 19 '15 at 11:43

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.