2

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);
            }
        }
    }
}
3
  • Please check question. Please approved the edit.It make more suitable question.
    – Amit Bera
    Commented Mar 7, 2014 at 12:59
  • @Amit Bera what's wrong with question ? can you be bit clear ? Commented Mar 7, 2014 at 13:45
  • no issue.this title make more suitable question
    – Amit Bera
    Commented Mar 7, 2014 at 13:47

1 Answer 1

0

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.

7
  • Thanks for your reply. I Made a module and followed your steps, and i can see my new module Enabled under system-> advanced -> Disable modules output. But no change in frontend ?? Commented Mar 9, 2014 at 14:36
  • Are you sure that code from observer is executed? Commented Mar 9, 2014 at 15:06
  • I'm not sure. I have mentioned my module files in above question. Can you have plz have a look. Commented Mar 9, 2014 at 15:41
  • Update answer plz take a look. Commented Mar 9, 2014 at 17:45
  • Is there anything needs to be done on frontend file i.e, upsell.phtml because i'm not getting any changes in frontend. I cleared cache and did all possible things. Any idea where its wrong ? Commented Mar 9, 2014 at 18:08

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.