Why does my Magento observer get stuck in and endless loop? - magento

My observer gets stuck in and endless loop. Why does it happen and how can I fix it?
config.xml:
<?xml version="1.0"?>
<config>
<global>
<models>
<shipmentsave>
<class>Company_Shipmentsave_Model</class>
</shipmentsave>
</models>
</global>
<adminhtml>
<events>
<sales_order_shipment_track_save_after>
<observers>
<shipmentsave>
<type>model</type>
<class>shipmentsave/observer</class>
<method>salesOrderShipmentTrackSaveAfter</method>
</shipmentsave>
</observers>
</sales_order_shipment_track_save_after>
<sales_order_shipment_save_after>
<observers>
<shipmentsave>
<type>model</type>
<class>shipmentsave/observer</class>
<method>salesOrderShipmentSaveAfter</method>
</shipmentsave>
</observers>
</sales_order_shipment_save_after>
</events>
</adminhtml>
<frontend>
<events>
<sales_order_shipment_save_after>
<observers>
<shipmentsave>
<type>singleton</type>
<class>shipmentsave/observer</class>
<method>salesOrderShipmentSaveAfter</method>
</shipmentsave>
</observers>
</sales_order_shipment_save_after>
</events>
</frontend>
</config>
Observer.php:
class Company_Shipmentsave_Model_Observer
{
public function salesOrderShipmentSaveAfter(Varien_Event_Observer $observer)
{
error_log("My observer called ....",0);
$shipment = $observer->getEvent()->getShipment();
$order = $shipment->getOrder();
$track = Mage::getModel('sales/order_shipment_track')
->setNumber('1231354564')
->setCarrierCode('localdelivery')
->setTitle('Aramex');
$shipment->addTrack($track);
$shipment->save();
return;
}
}

Your observer waits for a shipment to get saved, then saves a shipment, which causes it to receive another event (yadda yadda ad nauseum). You will need a way to either escape the loop or not have to save a shipment.
Can you move your event onto sales_order_shipment_save_before and then allow the normal shipment saving to take effect, or do you need the shipment to already have been saved to do your part of the logic?
If so, change the following line on your observer so that Magento uses it as a singleton:
<type>singleton</type> // changed from model
Then, create a variable in your class to track whether you've recursed yet. If so, then just return.
Alternatively, you could check to see if there are any tracking numbers already on the shipment and only save if there are not (and you add one). That will kill the recursion as well.
Let me know if one of those works for you.
Thanks,
Joe

I used
sales_order_shipment_save_after
and its working for me
Thanks :)

Related

How to Fetch OrderId After checkout in Magento

Hello I am creating a Observer in Magento to fetch OrderId
i have created an event name as in my config.xml
logically this event has to run after checkout
but in my case this event is running once i clicked on ADD TO CART
here is my config.xml file
<events>
<sales_quote_save_after>
<observers>
<salesorder>
<class>IlexSquare_SalesOrder_Model_Observer</class>
<method>salesOrder</method>
</salesorder>
</observers>
</sales_quote_save_after>
</events>
here is My Observer
class IlexSquare_SalesOrder_Model_Observer
{
public function salesOrder($observer)
{
die('7878');
}
}
But this die is running whenever i click on Add to Cart .. m missing something.
Please Help
After trying certain solution i got my answer
<events>
<checkout_type_onepage_save_order_after>
<observers>
<salesorder>
<class>IlexSquare_SalesOrder_Model_Observer</class>
<method>salesOrder</method>
</salesorder>
</observers>
</checkout_type_onepage_save_order_after>
</events>
just add this section in <frontend>tag instead of writing in <global> tag.

Magento: observer data not being passed to function?

I have written a magento observer that monitors the sales_order_place_after events in both the frontend and adminhtml portions of the site. The purpose of this is to simply grab the completed order info, and use it to send a secondary transactional email based on the country of shipping for the order.
I have setup my module config.xml like this:
<adminhtml>
<events>
<sales_order_place_after>
<observers>
<internationalemail>
<type>singleton</type>
<class>MGD_Internationalemail_Model_Observer</class>
<method>sendInternationalEmail</method>
</internationalemail>
</observers>
</sales_order_place_after>
</events>
</adminhtml>
<frontend>
<events>
<sales_order_place_after>
<observers>
<internationalemail>
<type>singleton</type>
<class>MGD_Internationalemail_Model_Observer</class>
<method>sendInternationalEmail</method>
</internationalemail>
</observers>
</sales_order_place_after>
</events>
</frontend>
And my model like this:
class MGD_Internationalemail_Model_Observer extends Varien_Event_Observer {
public function sendInternationalEmail($observer) {
Mage::log("---International Email Check ---");
Mage::log($observer->getOrder());
}
}
For some reason, when I submit a transaction, I get the "email check" in the log but the execution hangs and eventually times out, when I try to get the order and dump. No exception is thrown.
I cannot print_r or output $observer at all, which leads me to believe its not being populated. What am I missing here?
Thanks in Advance!
but the execution hangs and eventually times out
That's usually the result of an object being too large and/or having circular references, and PHP getting caught up trying to rendering the entire thing to a string.
Try something like one (or all) of these instead (possible typos, untested pseudo code)
Mage::Log(get_class($observer->getOrder()));
Mage::Log(array_keys($observer->getData()));
foreach($observer->getData() as $key=>$object)
{
Mage::Log($key . '::' . get_class($object));
}
//Mage::Log($observer->getOrder());

Magento: Creating an event to extend customer creation in magento

So I am simply trying to create a hook into registrations predispatch event. This is what I have so far:
config.xml:
<events>
<controller_action_predispatch_customer_account_createpost>
<observers>
<mymodulename>
<class>mymodulename/observer</class>
<method>hookToAccountCreationBefore</method>
</mymodulename>
</observers>
</controller_action_predispatch_customer_account_createpost>
</events>
and the observer:
Model/Observer.php :
public function hookToAccountCreationBefore($observer) {
die('getting here');
}
So I go and do a registration and I see the controller_action_predispatch_customer_account_createpost event getting called in my event logs but its not calling my function.
Please help!
UPDATE:
The answer below worked perfectly for me. However, $observer->getEvent()->getCustomer() is getting NULL for me even though another observer is overriding the same exact event and this works fine. I've tried temporarily commenting out the observer config for the other extension and its still empty. Any ideas?
In your config.xml you could also do
config.xml:
<global>
<models>
<mymodulename>
<class>MyNamespace_MyModuleName_Model</class>
</mymodulename>
</models>
<events>
<controller_action_predispatch_customer_account_createpost>
<observers>
<mymodulename>
<type>singleton</type>
<class>mymodulename/observer</class>
<method>hookToAccountCreationBefore</method>
</mymodulename>
</observers>
</controller_action_predispatch_customer_account_createpost>
</events>
</global>
or
<global>
<events>
<controller_action_predispatch_customer_account_createpost>
<observers>
<mymodulename>
<type>singleton</type>
<class>MyNamespace_MyModuleName_Model_Observer</class>
<method>hookToAccountCreationBefore</method>
</mymodulename>
</observers>
</controller_action_predispatch_customer_account_createpost>
</events>
</global>
Maybe try with following Event
customer_register_success

Product list event for prices?

I'm trying to discover how to configure an Observer to check when magento calls the prices when listing the products under categories/search results, but I can't find any clue about at the moment.
Anyone had this need before and can give me some pointers?
I'm using Magento 1.6.0.0.
One way you can do this is by observing the collection_load_after event for catalog products:
<catalog_product_collection_load_after>
<observers>
<Your_Module_Observer>
<type>model</type>
<class>your_module/Observer/class>
<method>modifyPrices</method>
</Your_Module_Observer>
</observers>
</catalog_product_collection_load_after>
You can then loop through the collection and get the prices for each product and make changes if you want:
$products = $observer->getCollection();
foreach( $products as $product )
{
$product->setPrice( $myCustomPrice );
}
Not sure if that's exactly what you are looking for, but hopefully it points you in the right direction.
a general mean is to set an observer for any specific event
see https://magento.stackexchange.com/questions/314/how-to-know-the-magento-event-that-we-want-to-hook
in the module Logevent, the config.xml
<?xml version="1.0" encoding="UTF-8"?>
<modules>
<Maticode_Logevent>
<version>0.1</version>
</Maticode_Logevent>
</modules>
<global>
<models>
<Logevent>
<class>Maticode_Logevent_Model</class>
</Logevent>
</models>
<events>
<controller_action_predispatch>
<observers>
<Logevent>
<type>singleton</type>
<class>Logevent/observer</class>
<method>controller_action_predispatch</method>
</Logevent>
</observers>
</controller_action_predispatch>
</events>
</global>
and the Model/observer.php
<?php
class Maticode_Logevent_Model_Observer {
public function controller_action_predispatch($observer) {
Mage::log ( $observer->getEvent ()->getControllerAction ()->getFullActionName (),null, 'eventlog.log' );
}
}
This way , in the
var/log/eventlog.log file
u can visualize a possible hook on any tested actions

Observer for Magent product after save

I am trying to add Observer to the Product status update event on backend. But it does not triggering the event.
<?xml version="1.0"?>
<config>
<modules>
<Mage4u_Customredirect>
<version>0.1.0</version>
</Mage4u_Customredirect>
</modules>
<global>
<events>
<catalog_product_status_update>
<observers>
<Mage4u_Customredirect_Catalog_product>
<type>singleton</type>
<class>mage4u_customredirect/observer</class> <method>on_catalog_product_status_update</method> </Mage4u_Customredirect_Catalog_product>
</observers>
</catalog_product_status_update>
</events>
</global>
</config>
And the observer function is to receive the status of the product that was updated and based on that i need to update it in another server.
<?php
class Mage4u_Customredirect_Model_Observer
{
public function on_catalog_product_status_update(Varien_Event_Observer $observer)
{
Mage::log("test " ,null,"test");
var_dump($observer);die();
}
}
?>
But it does not triggering this event. Please can someone tel me why its not working.
You should use the same notation here for class name as you use in Mage::getModel() factory method, e.g.: <class>mage4u_customredirect/observer</class>. Do not forget delete cache after this change to make it work.

Resources