How to trigger an event on payment received in magento?

烈酒焚心 提交于 2019-11-30 06:39:42

问题


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.


回答1:


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 !




回答2:


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.



来源:https://stackoverflow.com/questions/1365930/how-to-trigger-an-event-on-payment-received-in-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!