Magento observer - Which event to use when a coupon code is canceled - magento

I am building a module that adds a free product to the cart when a specific coupon code is entered.
I have an event observer which fires when a new coupon code is applied in the basket:
The event in my config is this:
<events>
<salesrule_validator_process>
<observers>
<checkoutApplyCouponToProduct>
<type>singleton</type>
<class>Sulman_Giftwithcoupon_Model_Checkout_Observer</class>
<method>applyCoupon</method>
</checkoutApplyCouponToProduct>
</observers>
</salesrule_validator_process>
</events>
This works correctly and I can add the free product succesully if the correct coupon code is added.
Now what I need to do is to remove the free product if the coupon code is canceled by the customer.
But the event I'm using does not fire when the coupon is canceled.
Is there an event I can use to check if the coupon code is removed?
Thanks

I would probably observe the controller_action_predispatch_checkout_cart_couponPost event and check whether the remove param is set, which is what triggers Mage_Checkout_CartController::couponPostAction to remove the coupon.
etc/config.xml
<config>
<global>
<events>
<controller_action_predispatch_checkout_cart_couponPost>
<observers>
<checkoutRemoveCouponProduct>
<type>singleton</type>
<class>Sulman_Giftwithcoupon_Model_Checkout_Observer</class>
<method>removeCoupon</method>
</checkoutRemoveCouponProduct>
</observers>
</controller_action_predispatch_checkout_cart_couponPost>
</events>
</global>
</config>
Model/Observer.php
public function removeCoupon(Varien_Event_Observer $observer)
{
/* #var Mage_Core_Controller_Front_Action $controller */
$controller = $observer->getControllerAction();
if ($controller->getRequest()->getParam('remove') == 1) {
// #TODO add logic to remove free product
}
return $this;
}

you can fire your custom event in couponPostAction() action in
app/code/core/Mage/Checkout/controllers/CartController.php
in this you will see a code which handles the wrong coupon and also if the coupon form has no value. You can fire your custom event there and listen that event in your module.
Hope this will do your work.
thanks

Related

Magento margin/profit rules to allow or prevent check out

Hi i have an attribute that calculates the margin of an product, in all attribute sets. Question is how can i set a rule to prevent customer check out if the margin(or profit) of a cart is less than required?
You can create an observer for the event controller_action_predispatch_checkout_onepage_index - this will fire when the customer starts the onepage checkout process.
This can be achieved like so in your modules config.xml:
<events>
<controller_action_predispatch_checkout_onepage_index>
<observers>
<YOUR_MODULE_checkout_onepage_index>
<type>singleton</type>
<class>YOUR_MODULE_Model_Observer</class>
<method>calculateProductMargin</method>
</YOUR_MODULE_checkout_onepage_index>
</observers>
</controller_action_predispatch_checkout_onepage_index>
</events>
If you are unaware about how to create a module then look here
So in your app/code/local/YOUR/MODULE/Model/Observer.php:
class YOUR_MODULE_Model_Observer extends Varien_Event_Observer {
public function setStore($observer) {
// Your logic here
}
}
In here you can $cart = Mage::getModel('checkout/cart')->getQuote(); and loop through the cart items, calculate your product margin and _goBack() potentially if it fails your requirements.
Enjoy! I hope this helps.

How do I access an eav attribute field defined and marked up in the billing step of Magento checkout?

I need to access it in an observer just before collectTotals() is run. I'm using the sales_quote_collect_totals_before observer and it seems to be doing everything else I need it to do. I want to be able to access this attribute when the event is triggered
You can use this sales_quote_address_collect_totals_before event in your module. like this:
<events>
<sales_quote_address_collect_totals_before>
<observers>
<namespace_your_modulename_custom_event>
<class>your_observer_class</class>
<method>yourObserverMethod</method>
</namespace_your_modulename_custom_event>
</observers>
</sales_quote_address_collect_totals_before>
</events>
In your observer model the method should look like this:
public function yourObserverMethod(Varien_Event_Observer $observer)
{
// code goes to here
}

redirect customer on custom page from Indexcontroller

I want to redirect customer even if he is not logged in, on my custom page where customer need to add quote and details of product. He will redirect on this page when he click on ‘Add to Compare’ link from product view page and after submitting form he will stay over there as it is with successful message.
Please note that,this thing I have to done from preDispatch function of Index Controller.
Thank you.
You need to add code in config.xml
<frontend>
<events>
<controller_action_predispatch>
<observers>
<controller_action_before>
<class>dispatcher/observer</class>
<method>controllerActionPreDispatch</method>
</controller_action_before>
</observers>
</controller_action_predispatch>
</events>
</frontend>
Add code in model/observer.php file
public function controllerActionPreDispatch($observer)
{
//we compare action name to see if that's action for which we want to add our own event
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'yourmodule Action Name')
{
Mage::app()->getResponse()->setRedirect(Mage::getUrl("customer/account"));
}
}

Alert or Commands or Notification if Quantity increase or Decrease in magento?

i need to get Alert or Commands or Notification message if Quantity increase or Decrease in magento? i am the new guy in magento?How to configure on this?
There only is an RSS feed notifying you if the quantity drops belowe a certain level. You would need to find or create an extension if you want a more sophisticated behavior.
Such an extension could be based on something like this:
You hook into the cataloginventory_stock_item_save_after event like this:
<events>
<cataloginventory_stock_item_save_after>
<observers>
<package_module_stocknote>
<type>singleton</type>
<class>Package_Module_Model_Observer</class>
<method>stocknote</method>
</package_module_stocknote>
</observers>
</cataloginventory_stock_item_save_after>
</events>
Then create an observer at Package/Module/Model/Observer.php:
class Package_Module_Model_Observer extends Varien_Event_Observer {
public function stocknote($observer) {
$stockItem = $observer->getEvent()->getItem();
//Insert your logic here
}
}

Magento observer goes in endless loop

Thanks in advance
I have created one observer i need to set the attribute values on fly using the observer please check the following config and the observer files when i click on the save button the observer goes into the endless . i just want to set the attribute value using this observer
<catalog_product_save_after>
<observers>
<zaptech_save_product_data>
<type>singleton</type>
<class>upload/observer_product</class>
<method>saveTabData</method>
</zaptech_save_product_data>
</observers>
</catalog_product_save_after>
and my observer handler code is here please check
public function saveTabData(Varien_Event_Observer $observer)
{
$productModel=Mage::registry('current_product')
->setTestid('1')
->setTestname('Jitendra')
->save();
}
the problem with this code is that the observe goes in endless loop
please help
Thanks again,
Jitendra Dhobi.
Here is the answe of my own question i replaced the event name from catalog_product_save_after to catalog_product_save_before..
<catalog_product_save_before>
<observers>
<zaptech_save_product_data>
<type>singleton</type>
<class>upload/observer_product</class>
<method>saveTabData</method>
</zaptech_save_product_data>
</observers>
</catalog_product_save_before>
and also remove the save() method from the observer file same below
public function saveTabData(Varien_Event_Observer $observer)
{
$productModel=Mage::registry('current_product');
$productModel->setTestid('1');
$productModel->setTestname('Jitendra');
}
cheers!!!...
You can use a registry to determinate your custom product save and prevent loop.
public function saveTabData(Varien_Event_Observer $observer)
{
if(Mage::registry('customUpdate')) return;
Mage::register('customUpdate', true);
$productModel=Mage::registry('current_product')
->setTestid('1')
->setTestname('Jitendra')
->save();
}

Resources