Question

i'm a newbie to magento, default using $this->getItemCollection()->getItems() (upsell.phtml) displaying all up selling products.

I'm wondering how to group these results by its category.

Example, i have 4 up sell products,

3 Products from category A,

1 Product from category B

On view i need to display like this,

Category A,

Product 1 | product 2 | Product 3

Category B,

Product 4

So far created a module, it's structure below,

local
   Fugen
     Accessories
        etc
          config.php
        Model
          Observer.php

My module's config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Fugen_Accessories>
            <version>1.0</version>
        </Fugen_Accessories>
    </modules>
    <frontend>
        <events>
            <catalog_product_upsell>
                <observers>
                    <Fugen_Accessories>
                        <class>Fugen_Accessories/observer</class>
                        <method>updateUpsells</method>
                    </Fugen_Accessories>
                </observers>
            </catalog_product_upsell>
        </events>
    </frontend>
</config>

My Observer.php

class Fugen_Accessories
{
    public function __construct()
    {
      // leave empty
    }

    public function updateUpsells(Varien_Event_Observer $oObserver)
    {
        $iCurrentCategory = Mage::registry('current_category')->getId();
        $oUpsellCollection = $oObserver->getCollection();
        foreach ($oUpsellCollection->getItems() as $key => $oUpsellProduct) {
            $aCategoriesIds = $oUpsellProduct->getCategoryIds();
            if (!in_array($iCurrentCategory, $aCategoriesIds)) {
                $oUpsellCollection->removeItemByKey($key);
            }
        }
    }
}
Was it helpful?

Solution

Take a look into Mage_Catalog_Block_Product_List_Upsell::_prepareData().

It throws event with upsell products collection.

Mage::dispatchEvent('catalog_product_upsell', array(
    'product'       => $product,
    'collection'    => $this->_itemCollection,
    'limit'         => $this->getItemLimit()
));

So we can create observer, attach to catalog_product_upsell event and change upsell products collection as we wish.

<?xml version="1.0"?>
<config>
    <modules>
        <Fugen_Accessories>
            <version>1.0</version>
        </Fugen_Accessories>
    </modules>
    <global>
        <models>
            <fugen_accessories>
                <class>Fugen_Accessories_Model</class>
            </fugen_accessories>
        </models>
    </global>
    <frontend>
        <events>
            <catalog_product_upsell>
                <observers>
                    <fugen_accessories>
                        <class>fugen_accessories/observer</class>
                        <method>updateUpsells</method>
                    </fugen_accessories>
                </observers>
            </catalog_product_upsell>
        </events>
    </frontend>
</config>

In observer create method

class Fugen_Accessories_Model_Observer
{
    public function updateUpsells(Varien_Event_Observer $oObserver)
    {
        $iCurrentCategory = Mage::registry('current_category')->getId();
        $oUpsellCollection = $oObserver->getCollection();
        foreach ($oUpsellCollection->getItems() as $key => $oUpsellProduct) {
            $aCategoriesIds = $oUpsellProduct->getCategoryIds();
            if (!in_array($iCurrentCategory, $aCategoriesIds)) {
                $oUpsellCollection->removeItemByKey($key);
            }
        }
    }
}

Also do not forget to add module initialization config in app/etc/modules

Fugen_Accessories.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Fugen_Accessories>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog/>
            </depends>
        </Fugen_Accessories>
     </modules>
</config>

Pay attention solution was not tested.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top