In the shopping cart we need to sort products in the order they are added and the grouped products need to be ordered alphabetically.

  • Any possibility to include the code you have tried so far?
    – Prateek
    Aug 5, 2015 at 19:36

1 Answer 1


We'll need to fix this in 2 parts. The first part is actually tracking which simple products in the cart come from grouped products. The second is the sorting.

First for the tracking. We'll add an extra column in sales_flat_quote_item to store that information.

To do this properly write your own module, a tutorial about that you can find here on Tutsplus.com.

You'll also need an install script. Check out this Inchoo tutorial about that.

Add the following in your install script

$installer = new Mage_Sales_Model_Resource_Setup('core_setup');

$installer->addAttribute('quote_item', 'is_grouped', array(
    'type'     => Varien_Db_Ddl_Table::TYPE_SMALLINT,
    'visible'  => false,
    'required' => false,
    'default'  => 0


This will be a simple 0 or 1 value.

Now we need to set that value when a product is added to the cart. We can do this with an observer.

Add the following in your config.xml


Now we need to know when added to cart this was a grouped product. We'll do that with a simple input field in the add to cart form.

Add the following in your modules layout XML. It will add an input field when the product type is grouped.

<?xml version="1.0"?>
<layout version="0.1.0">
        <reference name="product_options_wrapper">
            <block type="core/text" name="isgrouped.input">
                <action method="setText"><text><![CDATA[<input type="hidden" name="isgrouped" value="1"/>]]></text></action>

And an observer class that checks for the presence of the input.

class [Namespace]_[Module]_Model_Observer
    public function salesQuoteItemSetProduct($o)
        return $this;

    protected function _setIsGrouped($o)
        $isGrouped = (int)$this->getRequest()->getPost('isgrouped');
        if ($isGrouped) {
            $quoteItem = $o->getQuoteItem();

This will set the table field is_grouped to 1 when the simple product originates from a grouped product.

Now on to the sorting! The block class behind the cart is Mage_Checkout_Block_Cart which we'll need to rewrite from our own module. You can do that by adding the following to the config > global tag in your config.xml


And the getItems method can now filter out the grouped items and filter those alphabetically

class [Namespace]_[Module]_Block_Checkout_Cart extends Mage_Checkout_Block_Cart

     * Return customer quote items
     * @return array
    public function getItems()
        if ($this->getCustomItems()) {
            return $this->getCustomItems();

        $grouped = [];
        $items = parent::getItems();

         * Filter all the grouped items out to sort them
        foreach ($items as $i => $item) {
            if ($item->getIsGrouped() == 1) {
                $grouped["{$item->getName()}{$item->getId()}"] = $item;

        ksort($grouped, SORT_STRING); // sort key (which is name) alphabetically

        return array_merge($grouped, $items);

DISCLAIMER: untested code so might need some tweaking here or there but it should work in general. If not just comment


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.