I have a custom observer in Magento 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)) {

On echo $oUpsellCollection; i got nothing returned ?

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

  • try to get the collection like this: $oUpsellCollection = $oObserver->getEvent()->getCollection();
    – Marius
    Commented 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 ? Commented Mar 10, 2014 at 7:36

2 Answers 2


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...


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.


Either you can load the product yourself.


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


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 you have read our privacy policy.

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