I'm working on an existing magento page. A logged in user can change his profile informamation (firstname, lastname, email, etc.) and they can change their billing and shipping addresses.
What I need to do is send notification emails whenever a customer changes their basic information or one of their addresses. I created an observer for two events:
<frontend>
<events>
<customer_save_after>
<observers>
<ext_customer_save_after>
<type>singleton</type>
<class>ext/observer</class>
<method>customerSaveAfter</method>
</ext_customer_save_after>
</observers>
</customer_save_after>
<customer_address_save_after>
<observers>
<ext_customer_save_after>
<type>singleton</type>
<class>ext/observer</class>
<method>customerAddressSaveAfter</method>
</ext_customer_save_after>
</observers>
</customer_address_save_after>
</events>
</frontend>
And in customerSaveAfter I send an email and in customerAddressSaveAfter I check if the current ID is the same as the defaultbillingaddress or the defaultshipping address and send the notification accordingly. This is working fine, until the user checks the "Set as Default Shipping Address" checkbox. In this case, I suddenly receive 5 emails:
Billing Address changed
Shipping Address changed
Shipping Address changed
Billing address changed
customer info changed
So, suddenly the events are fired multiple times and the customer_address_save_after somehow triggers the customer_save_after event. Is there a way to prevent this or to check which event triggered another event or something like this? Or are there other ways to handle this problem?
I'd be really thankful for any hints, thank you very much.
I couldn't really solve the problem, with mage_registry I alawys got an error so I decided to go for a totally different approach.
In my extension I removed the observers and instead created 2 new Controllers, AccountController and AddressController, to overwrite Magentos Standard Controllers for handling customer and address saving.
In my config.xml I added this:
<frontend>
<routers>
<customer>
<args>
<modules>
<my_ext before="Mage_Customer_AccountController">My_Ext</my_ext>
<my_ext before="Mage_Customer_AddressController">My_Ext</my_ext>
</modules>
</args>
</customer>
</routers>
</frontend>
And, for example, my AccountController looks like this:
<?php
require_once Mage::getModuleDir('controllers','Mage_Customer').DS."AccountController.php";
class My_Ext_AccountController extends Mage_Customer_AccountController{
public function editPostAction(){
//I copied the code from the Magento AccountController here and at the proper line I added my own code
}
}
And the same for my AddressController. This worked really nicely.
Thanks for your help, everyone.
Related
I am trying to create a custom payment module in magento. I found a lot of tutorials for this, but only ones that just add the order. My question is: How can i set the order to paid when the user clicks on checkout.
This is the relevant code i have so far:
Config.xml
<?xml version="1.0"?>
<config>
<modules>
<My_Module>
<version>1.0.0.0</version>
</My_Module>
</modules>
<global>
<models>
<My_Module>
<class>My_Module_Model</class>
</My_Module>
</models>
</global>
<default>
<payment>
<mypayment>
<active>1</active>
<model>My_Module/Payment</model>
<order_status>processing</order_status>
<title>Testing</title>
</mypayment>
</payment>
</default>
</config>
Model/payment.php
<?php
class My_Module_Model_Payment extends Mage_Payment_model_Method_Abstract{
protected $_code = 'mypayment';
protected $_isInitializeNeeded = false;
protected $_canUseInternal = true;
protected $_canUseForMultishipping = true;
}
I also find it very hard to find documentation. For example, i am looking for a list of events, and a documentation for the payment method, but i do not seem to find anything. Does Magento not offer any of these?
I think you need to utilize the Varien_Object and Sales_Quote to do any custom payment of any sorts with Magento (not sure on this), and not just the abstract payment method.
Check this (the answer has a link to an actual working example of a custom payment module).
Magento custom payment method
thanks for reading, I'm trying to add new fields to newsletter in magento.
Made my search and found this answer which I think it's the most right one.
but when I added my observer, magento is not saving the new emails and some times the message
There was a problem with the subscription
appears.
here is the code:
config.xml(were my module is My_test in local):
<newsletter_subscriber_save_before>
<observers>
<class>test/newsletter_observer</class>
<method>add</method>
</observers>
</newsletter_subscriber_save_before>
and in file app/code/local/My/Test/Model/Newsletter/Observer.php :
class My_Test_Model_Newsletter_Observer{
public function add($observer){
// no thing here for now ..
}
}
can any one help??
thanx in advance.
#allGood, i see that you have missing events name
<newsletter_subscriber_save_before>
<observers>
<my_newsletter>
<class>test/newsletter_observer</class>
<method>add</method>
</my_newsletter>
</observers>
</newsletter_subscriber_save_before>
more details:http://blog.chapagain.com.np/magento-event-observer-with-save-before-and-save-after/
I have created a custom Module,In which i am Selling Recurring Product, if Customer buy and Recurring product then after placing Order Successfully,i am making some changes in Custom database table now i want if customer suspend that recurring profile from his/her id then my custom module database table must be updated according to that. is there any event observer that i can but on suspend button ?
You can use whatever event name you want in Magento. Don't know what do you mean by "suspending recurring profile" but when this happens you can fire/dispatch your custom event, for example:
Mage::dispatchEvent('stop_recurring_orders_save_after', array(
'some_id' => &$someId,
'other_fields' => &$otherFields,
));
You can find how to handle events in Magento here on SO.
Shortly in config.xml you need something like:
<config>
<global>
<events>
<stop_recurring_orders_save_after>
<observers>
<mycustomextension>
<type>singleton</type>
<class>mycustomextension/observer</class>
<method>stopRecurringOrders</method>
</mycustomextension>
</observers>
</stop_recurring_orders_save_after>
</events>
</global>
</config>
And an observer class with the appropriate method (e.g. stopRecurringOrders):
class Mycustomextension_Model_Observer {
public function stopRecurringOrders($event) {
$id = $event->getSomeId();
// update your custom table etc.
}
}
All the Events in Magento are available at
http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/magento_events
You can check them and select which suits your requirement....
Still learning magento coding. I wonder is there a way I can print out all the values in the collections on any page I load on magento? This would be assuming I don't know the names of the collections being used.
This would be very helpful if it is possible.
First, a word of warning - if you are var_dump'ing collections, let alone every collection loaded for a given request, then you are most likely going to run in to problems - collections contain huge amounts of data.
What is it exactly that you require from each collection that you would need to do this?
Anyway, the only way to get this data that comes to mind would be to use an observer subscribed to:
core_collection_abstract_load_after
So as a head start...
Your config.xml would look similar to this...
<?xml version="1.0"?>
<config>
<modules>
<YourCompany_YourModule>
<version>1.0.0</version>
</YourCompany_YourModule>
</modules>
<frontend>
<events>
<core_collection_abstract_load_after>
<observers>
<yourmodule>
<class>YourCompany_YourModule_Model_Observer</class>
<method>core_collection_abstract_load_after</method>
</yourmodule>
</observers>
</core_collection_abstract_load_after>
</events>
</frontend>
<global>
<models>
<yourmodule>
<class>YourCompany_YourModule_Model</class>
</yourmodule>
</models>
</global>
</config>
Your observer would look like this...
<?php
class YourCompany_YourModule_Model_Observer
{
public function core_collection_abstract_load_after(Varien_Event_Observer $observer)
{
$collection = $observer->getEvent()->getCollection();
//Do what you want with each collection here
}
}
A much simpler approach and be it your new to Magento I would suggest using this:
https://github.com/madalinoprea/magneto-debug
As it will offer other insight into the internal workings as well as collection and SQL info about the actual collection.
A view in action:
http://www.youtube.com/watch?v=aqvgrmebcu4
Greetings, in Magento I want to trigger an event, once an order has been set to processing (by gateway confirmation or manually) that, example: If a general customer (id 1) spends over 100$ and the payment has been confirmed, set his group id to 4 (silver VIP, which by promotion rule gets 2% discount globally)
I would give a bounty to this, but I'd like the answer before 2 days O_o
EDIT: the answer I received so far is only a partial answer, also I find the links very confusing, I'm not clear on what is the minimal setup, what do i have to configure create etc... Also I'm trying to find out how to get the paying customers id/model.
You should start by creating your own module in app/code/local.
Create for example the directories Moak/Vip. It will be the root of your module.
In order for Magento to know it exists, create a file named Moak_Vip.xml in etc/modules, with the following content :
<?xml version="1.0"?>
<config>
<modules>
<Moak_Vip>
<active>true</active>
<codePool>local</codePool>
<self_name>Moak VIP module</self_name>
</Moak_Vip >
</modules>
</config>
Then, in your module directory, you need the following structure and files :
etc/config.xml
Model/Observer.php
The config.xml defines your module and declares your event listener for a given event (checkout_onepage_controller_success_action is sent when onepage checkout process is complete, sales_order_payment_pay is sent when the payment has been confirmed).
You don't need any DB setup since you will not save any new entity.
So your config file should look something like the following :
<?xml version="1.0"?>
<config>
<modules>
<Moak_Vip>
<version>0.1.0</version>
</Moak_Vip>
</modules>
<global>
<models>
<moak>
<class>Moak_Vip_Model</class>
</moak>
</models>
<events>
<sales_order_payment_pay>
<observers>
<moak_observer>
<type>singleton</type>
<class>moak/observer</class>
<method>checkVipCustomer</method>
</moak_observer>
</observers>
</sales_order_payment_pay >
</events>
</global>
</config>
Now, your Observer method checkVipCustomer should receive an event object from which you can retrieve all information about the order, the customer... and perform the modifications you like.
Have a look at Magento model classes in app/code/core/Mage/.../Model/...
to see how to navigate through those objects.
Example :
<?php
class Moak_Vip_Model_Observer
{
public function checkVipCustomer($event)
{
$order = $event->getInvoice()->getOrder(); // Mage_Sales_Model_Order
/*
- Check order amount
- Get customer object
- Set Group id
- $customer->save();
*/
return $this;
}
}
Note I've not tested any of the code I wrote here, so handle with care !
Hope it helped, Magento has a hard learning curve...
Good luck !
You can create an observer for the "sales_order_payment_pay" event. Here is a cheatsheet of the events in magento 1.3.
And an explanation of how to create observer methods. Links courtesy of the excellent activecodeline and inchoo sites.