1

I have created one observer and its called when order placed. sales_order_save_after. Also i have created one sales attribute and try to set value when order placed. but it gives memory allocation error.


Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 36 bytes) in D:\xammp\htdocs\magento-1.9.3.0\lib\Zend\Db\Statement\Pdo.php on line 228

Bellow is my code.

config.xml

<events>
      <sales_order_save_after> <!-- identifier of the event we want to catch -->
        <observers>
          <sales_order_save_after_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>customeraffiliate/observer</class> <!-- observers class alias -->
            <method>saveAffiliateId</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </sales_order_save_after_handler>
        </observers>
      </sales_order_save_after>
    </events>

Observer.php

class Looxis_Customeraffiliate_Model_Observer
{
    public function saveAffiliateId(Varien_Event_Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        if($order->getId())
        {
            $orderModel = Mage::getModel('sales/order')->load($order->getId());
            //print_r($orderModel->getData());
            $orderModel->setAffiliateid(2);
            //print_r($orderModel->getData());die;
            $orderModel->save();
        }
    }       
}

Please help me why its give this error.

1 Answer 1

1

The event sales_order_save_after is called when save() function.

In your observer.. you have called the save() ($orderModel->save();) which is called sales_order_save_after again means there a recursive called happed at Observer that create memory issue

Use donot use load the order model again by

 $orderModel = Mage::getModel('sales/order')->load($order->getId());

Just change from

$order = $observer->getEvent()->getOrder();
        if($order->getId())
        {
            $orderModel = Mage::getModel('sales/order')->load($order->getId());
            //print_r($orderModel->getData());
            $orderModel->setAffiliateid(2);
            //print_r($orderModel->getData());die;
            $orderModel->save();
        }

to

$order = $observer->getEvent()->getOrder();
if($order->getId())
{
 $order->setAffiliateid(2);
    //print_r($orderModel->getData());die;
}

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.