I am trying to create controller_action_predispatch event:
This is my Mbyte/Pushnotification/etc/config.xml file code
<events>
<controller_action_predispatch_mbyte_pushnotification_index_index>
<observers>
<Mbyte_Pushnotification>
<class>Mbyte_Pushnotification/Observer</class>
<method>indexPreDispatch</method>
</Mbyte_Pushnotification>
</observers>
</controller_action_predispatch_mbyte_pushnotification_index_index>
</events>
My controller code : Mbyte/Pushnotification/controllers/IndexController.php file code
class Mbyte_Pushnotification_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
Mage::log('Run',null,'log.log');
}
}
Observer file code:
class Mbyte_Pushnotification_Model_Observer {
public function indexPreDispatch($observer)
{
Mage::log('Check',null,'observer.log');
}}
controller_action_predispatch is not working.
Is there something i am doing wrong?
The event triggered should be
controller_action_predispatch_pushnotification_index_index
instead of
controller_action_predispatch_mbyte_pushnotification_index_index
in case you did not modify the frontend name ("pushnotification") of your controller.
Therefore you have to update your config.xml to
<events>
<controller_action_predispatch_pushnotification_index_index>
<observers>
<Mbyte_Pushnotification>
<class>Mbyte_Pushnotification_Model_Observer</class>
<method>indexPreDispatch</method>
</Mbyte_Pushnotification>
</observers>
</controller_action_predispatch_pushnotification_index_index>
</events>
you could also write
<events>
<controller_action_predispatch_pushnotification_index_index>
<observers>
<Mbyte_Pushnotification>
<class>pushnotification/observer</class>
<method>indexPreDispatch</method>
</Mbyte_Pushnotification>
</observers>
</controller_action_predispatch_pushnotification_index_index>
</events>
if you named your module "pushnotification", which i assume.
According to magento define event depends should be
controller_action_predispatch_frontName_Controller_Youaction
it is define like:
Mage::dispatchEvent('controller_action_predispatch_' . $this->getFullActionName(),
array('controller_action' => $this));
Related
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.
Once an order is placed in my magento store I have a custom module so that i can save the order number into an external database table.
My custom module setup is:
Custom/ExternalOms/config.xml
So i hook into the sales_order_place_after event
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Custom_ExternalOms>
<version>0.0.1</version>
</Custom_ExternalOms>
</modules>
<global>
<models>
<custom_externaloms>
<class>Custom_ExternalOms_ExternalOms</class>
</custom_externaloms>
</models>
<events>
<sales_order_place_after>
<observers>
<custom_externaloms>
<type>model</type>
<class>Custom_ExternalOms_Model_ExternalOms</class>
<method>exportToOMS</method>
</custom_externaloms>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
Custom/ExternalOms/Model/ExternalOms.php
and run my function:
class Custom_ExternalOms_Model_ExternalOms extends Mage_Core_Model_Abstract
{
public function exportToOMS()
{
$_order_number = Mage::getSingleton('checkout/session')->getLastRealOrderId();
// remaining code..
}
}
The code runs once the order has been placed correctly but this:
Mage::getSingleton('checkout/session')->getLastRealOrderId();
Is coming up empty
Replace your event by this code
<events>
<sales_order_place_after>
<observers>
<custom_externaloms>
<type>model</type>
<class>externaloms/externalOms</class>
<method>exportToOMS</method>
</custom_externaloms>
</observers>
</sales_order_place_after>
</events>
ExternalOms file by this
class Custom_Externaloms_Model_ExternalOms {
public function exportToOMS($observer)
{
$observer->getOrder();
// remaining code..
}
}
/* in case $observer->getOrder(); will not work than use
Mage::getSingleton('checkout/session')->getLastOrderId(); for getting last order id and load order
*/
I hope it will work
Namespace_Modulename_Model_ObserverI need to create an event/observer to clear cart before a product is being added. The checkout process will include only one product. Could anybody give me a hand?
I have the following code so far, but I'm doing something wrong:
In config.xml I have:
<frontend>
<events>
<checkout_cart_product_add_after>
<observers>
<clear_cart_observer>
<type>singleton</type>
<class>Namespace_Modulename_Model_Observer</class>
<method>clearCart</method>
</clear_cart_observer>
</observers>
</checkout_cart_product_add_after>
</events>
</frontend>
Also, I have created an observer file which contains:
public function clearCart($observer)
{
foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ) {
Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}
}
I don't get any error in my system.log, but it doesn't trigger. Any ideas?
You likely need to fix this:
<class>Namespace_Modulename_Model_Observer</class>
And ensure that you have the module loaded in:
app/etc/modules/Namespace_Modulename.xml
Don't forget to flush cache.
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
What I doing wrong?
config.xml
...
<helpers>
<lacpaycs>
<class>OS_LacPayCS_Helper</class>
</lacpaycs>
</helpers>
</global>
<frontend>
<events>
<sales_model_service_quote_submit_before>
<observers>
<lacpaycs>
<type>singleton</type>
<class>OS_LacPayCS_Model_Observer</class>
<method>lacpaycs_payment_send</method>
</lacpaycs>
</observers>
</sales_model_service_quote_submit_before>
</events>
...
then in Observer Code:
public function lacpaycs_payment_send(Varien_Object $observer)
{
...
$helper = Mage::helper('laqpaycs');
and I getting error - magento tries to get helper from Mage/Laqpaycs/Helper/Data.php
How to say magento that it must get helper from OS/LaqPayCS/Helper/Data.php
You have a typo.
Your helper class group is <lacpaycs> but your helper factory argument is laqpaycs. Change your argument to lacpaycs and, provided that you have a class definition for OS_LacPayCS_Helper_Data at OS/LacPayCS/Helper/Data.php`, your class will be instantiated.