3

i wrote below observer to add extra price to product price based on get paramter id of product

 <catalog_product_load_after>
            <observers>
                <setcustomprice>
                    <type>singleton</type>
                    <class>Mour_Customgroup_Model_Observer</class>
                    <method>loadaftercatalog</method>
                </setcustomprice>
            </observers>
    </catalog_product_load_after>

observer.php

    public function loadaftercatalog(Varien_Event_Observer $event) {

            $pricetoadd=Mage::getModel('catalog/product')->load(233); // error caused after adding this line

            $product = $event->getEvent()->getProduct();
            $originalprice = $product->getPrice();
            $customprice = $originalprice+$pricetoadd->->getPrice();
            $product->setPrice($customprice);

   }

after adding code to observer i keep on getting error maximum nesting level of 500 reachesd i thing my observer i getting called more than once any better way to solve the issue

pls suggest

What i am trying to do :

1: there are two categories A, B if user browser any product of A he has option to buy it with any other product in B

so i pass id of product in A via get parameter and show Products of category B with added price based on id of product in get parameter

7
  • if($_GET['id']){} This wrong
    – Keyul Shah
    Jan 6, 2015 at 7:46
  • why soo i am passing id of product in get parameter Jan 6, 2015 at 7:47
  • you can get your Current Product Id using $observer ->getProduct(); you will get the product detail you do not required to load product again
    – Keyul Shah
    Jan 6, 2015 at 7:48
  • no i passing id of some other product also and want to add price of that product to cureent product Jan 6, 2015 at 7:50
  • then use Mage::app()->getRequest()->getParams() for fetching request data
    – Keyul Shah
    Jan 6, 2015 at 7:52

1 Answer 1

6

The event catalog_product_load_after is dispatched when calling Mage_Catalog_Model_Product::load.
In your code you call inside the observer

Mage::getModel('catalog/product')->load(233);

This will again dispatch the event catalog_product_load_after that will call the same code as above and will again dispatch the same event...and it will do this until it get tired. Apparently it gets tired after 500 loops.

I'm not sure what you are trying to achieve here, but for sure there is a better option.

but if you insist in using the code you have you can do something to stop your code from being executed when you call the second load.

Something like this:

public function loadaftercatalog(Varien_Event_Observer $event) {
    $product = $event->getEvent()->getProduct();
    if (!$product->getSkipCustomPriceObserver()) {
        $pricetoadd = Mage::getModel('catalog/product')
                          ->setSkipCustomPriceObserver(true) //this will set a flag that will be handled by the if statement above. If this flag is true your code will not be executed.
                          ->load(233); 
        $originalprice = $product->getPrice();
        $customprice = $originalprice+$pricetoadd->->getPrice();
        $product->setPrice($customprice);
     }
}
9
  • 2
    and then discard my answer.....need to type faster next time :) Jan 6, 2015 at 8:05
  • 3
    @DavidManners. I enhanced my typing and copy-pasting skills these last 2 weeks while on vacation :)
    – Marius
    Jan 6, 2015 at 8:06
  • 1
    I on the other hand forgot how to use a computer over the last two weeks. Jan 6, 2015 at 8:08
  • @DavidManners to be frank marius answering rate decreased after he turned into moderator.
    – dh47
    Jan 6, 2015 at 8:10
  • @dh47 Are you stalking me? :)
    – Marius
    Jan 6, 2015 at 8:23

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.