magento event listener - events

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.

Related

Magento 1.9 disable plugin for specific ip

I've got a Magento store (1.9) with a very simple plugin. It changes the customer group when someone places an order in a website. But we also work with a POS.
In the POS the plugin is also trying to change the customer group but it gives an error.
So what I want to do is disable the plugin for our local IP (or User).
The code is the following or check Github:
observer.php
<?php
class RvdH_GroupChange_Model_Observer
{
public function changeGroup(Varien_Event_Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
/*$event = $observer->getEvent(); //Fetches the current event"
$customer = $event->getCustomer();
$dbcustomer = Mage::getModel('customer/customer')->load($customer[entity_id]);*/
// ensure it's not guest checkout
if ($customer->getId()) {
$customer->setGroupId(5);
$customer->save();
}
}
}
config.xml
<?xml version="1.0"?>
<config>
<modules>
<RvdH_GroupChange>
<version>0.1.0</version>
</RvdH_GroupChange>
</modules>
<global>
<models>
<RvdH_GroupChange>
<class>RvdH_GroupChange_Model</class>
<resourceModel>module_mysql4</resourceModel>
</RvdH_GroupChange>
</models>
<events>
<sales_order_place_after>
<observers>
<RvdH_GroupChange>
<class>RvdH_GroupChange_Model_Observer</class>
<method>changeGroup</method>
</RvdH_GroupChange>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
Solved.
I just had to change the <global> tag to <frontend> in the config.xml.
Now it only works in frontend and not global.

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.

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

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

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 :)

Resources