Which Event call on Clear Shopping Cart Button Magento - magento

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.

Related

Set Payment Method "Cash On Delivery" on Particular State Only

I want to set "Cash On Delivery" for the site owner's State only
I know how to set For "Specified Country"
I am using Magento 1.8
How can I achieve this ?
LuFFy,you doing this using magento event observer:
Create an extension and here the step:
create config.xml under :app/code/community/Devamitbera/Statewisecod/etc/config.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
#Author Amit Bera
#Email dev.amitbera#gmail.com
# Website: www.amitbera.com
*/-->
<config>
<modules>
<Devamitbera_Statewisecod>
<version>1.0.0</version>
</Devamitbera_Statewisecod>
</modules>
<global>
<models>
<statewisecod>
<class>Devamitbera_Statewisecod_Model</class>
</statewisecod>
</models>
</global>
<frontend> <!-- run observer event for frontend -->
<events>
<payment_method_is_active>
<observers>
<enable_cod_for_some_state>
<class>statewisecod/observer</class>
<method>EnableCod</method>
</enable_cod_for_some_state>
</observers>
</payment_method_is_active>
</events>
</frontend>
</config>
Create observer file
under Observer.php under app/code/community/Devamitbera/Statewisecod/Model
Code of this file:
<?php
class Devamitbera_Statewisecod_Model_Observer
{
public function EnableCod($observer){
$result=$observer->getEvent()->getResult();
$MethodInstance=$observer->getEvent()->getMethodInstance();
$quote=$observer->getEvent()->getQuote();
if($quote && $quote->getId()):
/* If Payment method is cashondelivery then conitnue */
if($MethodInstance->getCode()=='cashondelivery'){
#Mage::log('Payment is Cod',null,'Cod.log',true);
$ShippingAddress=$quote->getShippingAddress();
/* region_id is working when country have
* drop state/regions.
*/
/* Here i have put USA coutry new work & Washinton redion */
#Mage::log('redion'.$ShippingAddress->getRegionId(),null,'redion.log',true);
$CodEnableRegionIds=array(62,43);
if(in_array($ShippingAddress->getRegionId(),$CodEnableRegionIds)):
$result->isAvailable=true;
elseif(is_null($ShippingAddress->getRegionId()) && !is_null($ShippingAddress->getRegion())):
/* This section working when State/region is not dropdown
and state is dropdown
*/
$textListRegionName=array('West bengal','Delhi');
if(in_array($ShippingAddress->getRegion(),$textListRegionName)){
$result->isAvailable=true;
}else{
$result->isAvailable=false;
}
else:
$result->isAvailable=false;
endif;
return $result->isAvailable;
}
endif;
}
}
create module file Devamitbera_Statewisecod.xml under app/etc/modules
<?xml version="1.0" encoding="utf-8"?>
<!--
#Author Amit Bera
#Email dev.amitbera#gmail.com
# Website: www.amitbera.com
-->
<config>
<modules>
<Devamitbera_Statewisecod>
<codePool>community</codePool>
<active>true</active>
<depends><Mage_Payment/></depends>
</Devamitbera_Statewisecod>
</modules>
</config>
Here cashondelivery is payment method code of cash on delivery.... which is saved in database.
- Edited:
region_id is working when country have drop state/regions list.
if(in_array($ShippingAddress->getRegionId(),$CodEnableRegionIds)):
$result->isAvailable=true;
If State/region is not dropdown then Below logic is work
elseif(is_null($ShippingAddress->getRegionId()) && !is_null($ShippingAddress->getRegion())):
$textListRegionName=array('West bengal','Delhi');
if(in_array($ShippingAddress->getRegion(),$textListRegionName)){
$result->isAvailable=true;
}else{
$result->isAvailable=false;
}

magento how to change cart account in checkout page

in product detail page, the product price is 50 dollar, I use JavaScript change the price to 80 dollar, but when add to cart, it is still 50 dollar in checkout page. how to let it still 80 dollar in checkout page?
You need use "sales_quote_add_item" magento event to update the product price in cart session.
You have to make a custom module for that purpose.
Create a file in app/etc/modules/Company_All.xml
<?xml version="1.0"?> <config> <modules>
<Company_Product>
<codePool>local</codePool>
<active>true</active>
</Company_Product> </modules> </config>
Create configuration file for our module file in app/code/local/Company/Product/etc/config.xml
<?xml version="1.0"?> <config> <global>
<models>
<product>
<class>Company_Product_Model</class>
</product>
</models>
<events>
<sales_quote_add_item><!--Event to override price after adding product to cart-->
<observers>
<company_product_price_observer><!--Any unique identifier name -->
<type>singleton</type>
<class>Company_Product_Model_Price_Observer</class><!--Our observer class name-->
<method>update_book_price</method><!--Method to be called from our observer class-->
</company_product_price_observer>
</observers>
</sales_quote_add_item>
</events> </global> </config>
Create our observer file in app/code/local/Company/Product/Model/Price/Observer.php
class Company_Product_Model_Price_Observer{
public function update_book_price(Varien_Event_Observer $observer) {
$quote_item = $observer->getQuoteItem();
//if(){ //your logic goes here
$customprice = 50;
//}
$quote_item->setOriginalCustomPrice($customprice);
$quote_item->save();
return $this;
}
}

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.

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.

Resources