My observer does not seem to be catching events emitted by Magento v1.12.0.2. I'm following a tutorial very closely from http://inchoo.net/ecommerce/magento/dispatching-before-and-after-events-to-magento-core-actions/ and cannot seem to reproduce.
app/etc/modules/Require_Additional_Product.xml:
<?xml version="1.0"?>
<config>
<modules>
<RequireAdditionalProduct>
<active>true</active>
<codePool>local</codePool>
</RequireAdditionalProduct>
</modules>
</config>
app/code/local/Hatclub/RequireAdditionalProduct/etc/config.xml:
<modules>
<RequireAdditionalProduct>
<version>1.0.0</version>
</RequireAdditionalProduct>
</modules>
<global>
<models>
<dispatcher>
<class>Hatclub_RequireAdditionalProduct_Model</class>
</dispatcher>
</models>
<events>
<!-- Hooking to Magento's default event "controller_action_predispatch" -->
<controller_action_predispatch>
<observers>
<controller_action_before>
<class>dispatcher/observer</class>
<method>hookToControllerActionPreDispatch</method>
</controller_action_before>
</observers>
</controller_action_predispatch>
<!-- Hooking to Magento's default event "controller_action_postdispatch" -->
<controller_action_postdispatch>
<observers>
<controller_action_after>
<class>dispatcher/observer</class>
<method>hookToControllerActionPostDispatch</method>
</controller_action_after>
</observers>
</controller_action_postdispatch>
<!-- Hooking to our own event "add_to_cart_before" -->
<add_to_cart_before>
<observers>
<add_to_cart_before>
<class>dispatcher/observer</class>
<method>hookToAddToCartBefore</method>
</add_to_cart_before>
</observers>
</add_to_cart_before>
<!-- Hooking to our own event "add_to_cart_after" -->
<add_to_cart_after>
<observers>
<add_to_cart_after>
<class>dispatcher/observer</class>
<method>hookToAddToCartAfter</method>
</add_to_cart_after>
</observers>
</add_to_cart_after>
</events>
</global>
app/code/local/Hatclub/RequireAdditionalProduct/Model/Observer.xml:
class Hatclub_RequireAdditionalProduct_Model_Observer {
//this is hook to Magento's event dispatched before action is run
public function hookToControllerActionPreDispatch($observer)
{
error_log('test 1', 0);
//we compare action name to see if that's action for which we want to add our own event
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')
{
//We are dispatching our own event before action ADD is run and sending parameters we need
Mage::dispatchEvent("add_to_cart_before", array('request' => $observer->getControllerAction()->getRequest()));
}
}
public function hookToControllerActionPostDispatch($observer)
{
error_log('test 1', 0);
//we compare action name to see if that's action for which we want to add our own event
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')
{
//We are dispatching our own event before action ADD is run and sending parameters we need
Mage::dispatchEvent("add_to_cart_after", array('request' => $observer->getControllerAction()->getRequest()));
}
}
public function hookToAddToCartBefore($observer)
{
error_log('test 1', 0);
//Hooking to our own event
$request = $observer->getEvent()->getRequest()->getParams();
// do something with product
Mage::log("Product ".$request['product']." will be added to cart.");
}
public function hookToAddToCartAfter($observer)
{
error_log('test 1', 0);
// Hooking to our own event
$request = $observer->getEvent()->getRequest()->getParams();
// do something with product
Mage::log("Product ".$request['product']." is added to cart.");
}
}
I'm simply trying to get output so that I know the observer is catching the event (through the use of php error_log, and Mage::log). Neither are outputting anything, so it seems that this is not working at all. Can anyone spot where I'm going wrong?
The xpath from your module registration file is incorrect for your module config file.
<?xml version="1.0"?>
<config>
<modules>
<!--
this node name along with codePool value is how your module's
config.xml file will be located.
-->
<Hatclub_RequireAdditionalProduct>
<active>true</active>
<codePool>local</codePool>
</Hatclub_RequireAdditionalProduct>
</modules>
</config>
See Mage_Core_Model_Config::loadModulesConfiguration() (link) for more insight.
Related
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
I want to call event/observer when user click on "clear shopping cart button" which perform some action on database. i search a lot but didn't find any specific solution.
please anyone will give me the solution that Which event is calling on clear shopping cart button in magento?
Try this event controller_action_predispatch_checkout_cart_updatePost.
And your config.xml file should be,
<?xml version="1.0"?>
<config>
<modules>
<Packagename_ModuleName>
<version>0.1.0</version>
</Packagename_ModuleName>
</modules>
<global>
<helpers>
<modulename>
<class>Packagename_ModuleName_Helper</class>
</modulename>
</helpers>
<models>
<modulename>
<class>Packagename_ModuleName_Model</class>
<resourceModel>modulename_mysql4</resourceModel>
</modulename>
</models>
<events>
<controller_action_predispatch_checkout_cart_updatePost> <!-- identifier of the event we want to catch -->
<observers>
<controller_action_predispatch_checkout_cart_updatePost_handler> <!-- identifier of the event handler -->
<type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
<class>modulename/observer</class> <!-- observers class alias -->
<method>clearCart</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</controller_action_predispatch_checkout_cart_updatePost_handler>
</observers>
</controller_action_predispatch_checkout_cart_updatePost>
</events>
</global>
</config>
And Model/Observer.php :
<?php
class Packagename_ModuleName_Model_Observer
{
public function clearCart(Varien_Event_Observer $observer)
{
//execute only in empty the cart function(all items removed )
$updateAction = (string)Mage::app()->getRequest()->getParam('update_cart_action'); if ($updateAction != 'empty_cart') return;
echo "got it"; exit;
//your stuffs goes here.
}
}
Note: It is not triggered when we clear single cart (product) item . I tested it in my localserver and it is working fine.
I'm creating a Magento module and I want the Observer to trigger when my custom orderstatus is chosen.
Practical situation:
People select: 'Payment Received' the //DO STUFF in the Observer is triggered.
This ain't working, so what is wrong here? (the status shows up correctly though) :-(
config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MOD_PaidStatus>
<version>1.0.0</version>
</MOD_PaidStatus>
</modules>
<global>
<sales>
<order>
<statuses>
<payment_received translate="label"><label>Payment Received</label></payment_received>
</statuses>
<states>
<processing translate="label">
<label>Processing</label>
<statuses>
<processing default="1"/>
<payment_received default="2" />
</statuses>
<visible_on_front/>
</processing>
<pending translate="label">
<label>Pending</label>
<statuses>
<pending default="1"/>
<payment_received default="2" />
</statuses>
<visible_on_front/>
</pending>
</states>
</order>
</sales>
<events>
<sales_order_resource_save_attribute_after>
<observers>
<PaidStatus>
<class>MOD_PaidStatus/observer</class>
<method>observeAttributeChange</method>
<type>singleton</type>
</PaidStatus>
</observers>
</sales_order_resource_save_attribute_after>
</events>
</global>
</config>
And my Observer.php looks like:
class MOD_PaidStatus_Model_Observer {
public function observeAttributeChange($observer){
$attribute = $observer->getEvent()->getAttribute();
if ($attribute->getAttributeCode() == 'status') {
mail('test#testcase.com', 'testcase', 'foo bar?');
}
}
}
In order to be able to execute some actions on changing the order's attribute "status" it is preferable to observe an event. In this case the event would be sales_order_resource_save_attribute_after. The observer function could be set up as follows:
class YourPackage_YourModule_Model_Observer {
public function observeAttributeChange($observer)
{
$attribute = $observer->getEvent()->getAttribute();
if ($attribute->getAttributeCode() == 'status') {
// DO STUFF
}
}
In your config.xml enter the necessary observer configuration
<global>
<events>
<sales_order_resource_save_attribute_after>
<observers>
<your_observer_node_name>
<class>yourpackage_yourmodule/observer</class>
<method>observeAttributeChange</method>
<type>singleton</type>
</your_observer_node_name>
</observers>
</sales_order_resource_save_attribute_after>
</events>
</global>
You can do this with rewriting or observer.
You do a kind of mix of these 2 concepts.
To use rewrite, do not name your file Observer, use something like Order.php :
class MOD_PaidStatus_Model_**Order** extends Mage_Sales_Model_Order
and in your config.xml, in the global node, add this :
<models>
<sales>
<rewrite>
<order>MOD_PaidStatus_Model_Order</order>
</rewrite>
</sales>
</models>
If you want to go through an Observer, use the name Observer.php and the classname should be :
MOD_PaidStatus_Model_Observer
and it shouldn't extends anything.
The function is more like
public function myEventHandling($event)
{
$order = $event->getOrder();
$status = $order->getStatus();
//DO YOUR STUFF here
}
But your should listen for a order_status_changed_after event that doesn't exist, so you add to trigger it too... Very big stuff if you're not a confirmed developer.
If you want something like this, you need an event node in your config.xml file to associate your Observer and its method to the event. Make some search about event handling in Magento
Regards,
Edit.: for observer and event way, look the previous post. But handling such a generic event is a very resource consuming way.
G'Day all,
I'm trying to write my first magento event handler to capture "checkout_cart_save_after" but I can't get my Observer.php code to fire, after 2 days of looking at web sites on magento events I understand the whole process but the code is not running.. I'm perplexed as to why.
Here is the modules xml file in /var/magento/app/etc/modules/
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Yourmodule>
<codePool>local</codePool>
<active>true</active>
<version>1.0.0</version>
</Namespace_Yourmodule>
</modules>
</config>
As you can see I'm just using Namespace and Yourmodule as I'm writing a tutorial for others....
In my module's etc directory (/var/magento/app/code/local/Namespace/Yourmodule/Module) I have config.xml
<?xml version="1.0"?>
<global>
<events>
<checkout_cart_save_after>
<observers>
<yourmodule_after_observer>
<type>singleton</type>
<class>Namespace_Yourmodule_Model_Observer</class>
<method>checkout_cart_save_after</method>
</yourmodule_after_observer>
</observers>
</checkout_cart_save_after>
</events>
</global>
and in the Modules directory I have my Observer.php file:
class Namespace_Yourmodule_Model_Observer
{
/*----------------------------------------
*
* LogInfo()
*
* Basic logging of activity to disk for debugging
*/
public function LogInfo($msg)
{
$logfile="/logs/Namespace-module.log";
$fd=fopen($logfile,"a");
if($fd)
{
fwrite($fd,$msg."\n");
fclose($fd);
}
}
public function checkout_cart_save_before($observer)
{
LogInfo("save_before called");
}
public function checkout_cart_save_after($observer)
{
LogInfo("save_after called");
}
}
The result of trying to add and remove items from the cart is that no log file is crearted and when manually created, no data is written, system.log shows no errors, if I skew the XML it reports the errror, so the XML file is being read.
Any thoughts on what I have missed???
Sid
Update 12/2:
I have spent the weekend on this and solved the issue, thanks for the hints, it was a combination of minor things that I should have picked up and the format of the XML file now matches what has been suggested above... there is now a tutorial! I have fully documented the process with working code and config files.
See http://z900collector.wordpress.com/magento/magento-events/
Try updating your config.xml to
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Yourmodule>
<version>1.0</version>
</Namespace_Yourmodule>
</modules>
<global>
<!--helpers>
<yourmodule>
<class>Namespace_Yourmodule_Helper</class>
</yourmodule>
</helpers-->
<models>
<yourmodule>
<class>Namespace_Yourmodule_Model</class>
</yourmodule>
</models>
<events>
<checkout_cart_save_after>
<observers>
<yourmodule_after_observer>
<type>singleton</type>
<class>Namespace_Yourmodule_Model_Observer</class>
<method>checkout_cart_save_after</method>
</yourmodule_after_observer>
</observers>
</checkout_cart_save_after>
</events>
</global>
</config>
See Customize Magento using Event/Observer
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.