3

I need to set a particular product as related product for all the products.Here is the code that I've tried.

$product=$observer->getProduct();
$id=52;
$data=array(
            $id=>array(
                'position'=>2
            )
        );
$p=Mage::getModel('catalog/product')->load($product->getId());
$p->setRelatedLinkData($data);
$p->save();

But I get error

Fatal error: Maximum function nesting level of '100' reached, aborting! in /var/www/magento/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php on line 205

Any pointers would be helpful.

2
  • 2
    what event are you observing? sounds like you ended up with a recursion in there... Commented Jan 21, 2014 at 11:24
  • Tried both catalog_product_save_after & catalog_product_save_before
    – blakcaps
    Commented Jan 21, 2014 at 12:28

3 Answers 3

5

Use catalog_product_save_before... and update the code so it doesn't end up in an infinite loop.

$product=$observer->getProduct();
$id=52;
$data=array(
            $id=>array(
                'position'=>2
            )
        );
$product->setRelatedLinkData($data);
0
1

A simple solution solved my problem. I just commented the:

"zend_extension = "d:/wamp/bin/php/php5.3.8/zend_ext/php_xdebug-2.1.2-5.3-vc9.dll"

in php.ini file. This extension was limiting the stack to 100 so I disabled it. The recursive function is now working as anticipated.

0

An alternative approach would be to add this product dynamically to the product collection every time the related products are loaded. This way you don't need to save all your products and have more flexibility:

  1. Observe eav_collection_abstract_load_before
  2. Check if it is the related products collection:

    $c = $observer->getCollection();
    if ($c instanceof Mage_Catalog_Model_Resource_Product_Link_Product_Collection &&
        $c->getLinkModel()->getLinkTypeId() === Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED
    ) {
    
  3. If so, add the product to the collection with an additional condition:

    $c->getSelect()->orWhere($c->getConnection()->prepareSqlCondition('e.entity_id', 52);
    

Of course you won't hard code the product id 52 like this, but retrieve it from configuration or somewhere else.

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.