1

I have a custom observer in Magento 1.8.1.0 that is called on Product view page when current product having any upsell products. I have verified (using Mage::log()) that the observer is working, however when I try the following:

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

On echo $oUpsellCollection; i got nothing returned ?

Is anyone know how to get upsell products collection ? Is this a proper way to do it ?

2
  • try to get the collection like this: $oUpsellCollection = $oObserver->getEvent()->getCollection();
    – Marius
    Mar 10, 2014 at 6:53
  • @Marius I think it's working, using print_r($oUpsellCollection) giving me huge array of data's. Now using above foreach loop i'm trying to get category ids, but it's not working ? Is my foreach loop execution is fine ? Mar 10, 2014 at 7:36

2 Answers 2

1

Instead of using getUpSellProductCollection(), use getUpSellProducts() as it have some sort of cache. For the category validation try instead getCategoryCollection() but the getCategoryIds() method should work, make sure your product is associated to categories... and that $oUpsellProduct is instance from Mage_Catalog_Model_Product or is extended classes...

0

The reason that you are getting nothing from the the upselling collection is you are calling it on an empty model. You are calling the following without loading a product.

 Mage::getModel('catalog/product')->getUpSellProductCollection()

Either you can load the product yourself.

Mage::getModel('catalog/product')->load(product_id)->getUpSellProductCollection()

Or if you are on the product view page you should be able to call the product from the registry.

Mage::registry('product')->getUpSellProductCollection()

Or finally if the event you have passes in the product you should be able to get the product from the event. But without knowing the event and how it is called I cannot guess what you need to get from $oObserver->getEvent()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.