Observer for Magent product after save - magento

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.

Related

Magento - Getting 'getLastRealOrderId' once order has been placed

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

Magento trigger observer with custom orderstatus

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.

Catching Magento Events With Observer

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.

magento event listener

I don't want to apply the discount coupon on the products already discounted i.e on products having special price . So i created a event listen as :
<?xml version="1.0" ?>
<config>
<modules>
<Tweett_Fashionn>
<version>0.1.0</version>
</Tweett_Fashionn>
</modules>
<global>
<events>
<salesrule_validator_process>
<observers>
<Tweett_Fashionn_Hook>
<type>singleton</type>
<class>Tweett_Fashionn_Model_Observer</class>
<method>specialpricediscount</method>
</Tweett_Fashionn_Hook>
</observers>
</salesrule_validator_process>
</events>
</global>
and the observer file as
<?php
class Tweett_Fashionn_Model_Observer extends Varien_Event_Observer{
public function __construct(){
echo "<p style='color:red'>Hello World .. </p>";
}
public function specialpricediscount($observer){
$item=$observer['item'];
$_product = Mage::getSingleton('catalog/product')->load($item->getProductId());
if($_product->getSpecialPrice()>0 ){
$result = $observer['result'];
$result->setDiscountAmount(0);
}
}
}
?>
but it dont even print the hello world when i click on apply coupon button ..plz help
You're missing the ending from your config.xml? You may have just not pasted it in.
Have you made sure your module is enabled?
Take a look at this free extension it can help you debug extensions - http://www.magentocommerce.com/magento-connect/magneto-debug-8676.html
I had same issue.I think following is correct way :
<salesrule_validator_process>
<observers>
<Namespace_Modulename> // your namespace_modulename
<type>model</type> //change singleton to model
<class>Tweett_Fashionn_Model_Observer</class>
<method>specialpricediscount</method>
</Namespace_Modulename>
</observers>
</salesrule_validator_process>
May this will help you.

How do i override the "addAction()" funciton under app\code\core\Mage\Checkout\controllers

I am trying to add some data to a session variable when a product is added to the shopping cart...
I tried copying the "CartController.php" under "app\code\core\Mage\Checkout\controllers" to
"local\Mage\Checkout\controllers" but its not actually overriding the controller..
Could anyone give me some hint on this..?
Thanks a lot.
You can create an "Event Oberserver" to trigger an event.
Create the following folders :
app/code/local/[myMagento]/EventTest/etc
app/code/local/[myMagento]/EventTest/Model
create a file under /EventTest/Model directory like myMagento_EventTest.xml and put the following :
<?xml version="1.0"?>
<config>
<modules>
<myMagento_EventTest>
<active>true</active>
<codePool>local</codePool>
</myMagento_EventTest>
</modules>
</config>
create config.xml file under /EventTest/etc folder nad put the following :
<?xml version="1.0"?>
<config>
<modules>
<myMagento_EventTest>
<version>0.1.0</version>
</myMagento_EventTest>
</modules>
<frontend>
<events>
<checkout_cart_product_add_after>
<observers>
<myMagento_EventTest_Model_Observer>
<type>singleton</type>
<class>myMagento_EventTest_Model_Observer</class>
<method>MyTestMethod</method>
</myMagento_EventTest_Model_Observer>
</observers>
</checkout_cart_product_add_after>
</events>
</frontend>
</config>
create model class file observer.php under the /EventTest/Model directory and put following
<?php
class myMagento_EventTest_Model_Observer {
public function MyTestMethod($observer) {
$event = $observer->getEvent(); //Fetches the current event
$product = $event->getProduct();
$eventmsg = "Current Event Triggered : " . $event->getName() . "
Currently Added Product : " . $product->getName();
//Adds Custom message to shopping cart
echo Mage::getSingleton("checkout/session")->addSuccess($eventmsg);
//Your Custom Logic Here
//you can use print_r($product) here to get more details
}
}
I found this , this enables us to override the CartController.php addAction() funciton... This is exaclty i needed.
Hope this would help someone in need.
Balan

Resources