How to trigger an event on payment received in magento?

前端 未结 2 647
闹比i
闹比i 2020-12-14 13:12

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)

2条回答
  •  北海茫月
    2020-12-14 13:43

    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 :

    
    
        
            
                true
                local
                Moak VIP module
            
        
    
    

    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 :

    
    
        
            
                0.1.0
            
        
        
            
                
                    Moak_Vip_Model
                
                  
            
                
                    
                        
                            singleton
                            moak/observer
                            checkVipCustomer
                        
                    
                     
            
         
    
    

    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 :

    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 !

提交回复
热议问题