在付款捕获后调用Magento事件观察者

时间:2013-10-16 01:19:07

标签: php magento

我尝试在创建订单后调用Observer,并在捕获付款后付款。 到目前为止我已经尝试过; checkout_submit_all_after, sales_order_payment_place_end, sales_order_place_after, sales_order_payment_pay, sales_order_payment_capture, sales_order_payment_transaction_save_after

仅举几个主要名称。 我还记录了dispatchEvent()中的所有Event Dispaches,但发现什么都没有突出,只在我需要它时被触发。 我遇到的问题是,订单的状态总是以太网“付款待定”或之前的事情;意思是我不知道订单是否会失败或成功。

我的目标是仅在成功订单上启动功能。 感谢。

3 个答案:

答案 0 :(得分:6)

经过更多测试后,我发现以下观察者可以做到这一点;

checkout_onepage_controller_success_action

这只返回订单ID,所以;

$order_id = $observer->getData('order_ids');
$order = Mage::getModel('sales/order')->load($order_id);

您看到订单状态为“正在处理”且付款已被批准(或未付款)。

答案 1 :(得分:3)

1)这里是调用观察者文件的自定义config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>0.1.0</version>
        </Namespace_Modulename>
    </modules>
    <frontend>
        <events>            
            <checkout_submit_all_after>
                <observers>
                    <Namespace_Modulename_Customevent>
                        <type>singleton</type>
                        <class>Namespace_Modulename_Model_Observer</class>
                        <method>customFunction</method>
                    </Namespace_Modulename_Customevent>
                </observers>
            </checkout_submit_all_after>
        </events>
    </frontend>    
</config>

2)在您的module / Model目录中创建observer.php文件并粘贴此代码

<?php
  class Namespace_Modulename_Model_Observer
{
    public function customFunction(Varien_Event_Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
         //here you can add your custom code

    }        

}

请试试这个..确定它会对你有帮助!

答案 2 :(得分:0)

我也尝试了所有活动,但没有取得成功。然后我移动覆盖Mage OnePageController并调用我的自定义函数。以下是覆盖onetep checkout的代码。

应用\等\模块\ Namespace_Module.xml

<Namespace_Checkout>
    <active>true</active>
    <codePool>local</codePool>
</Namespace_Checkout>

应用\代码\本地\命名空间\结帐\等\ config.xml中

<?xml version="1.0"?>
<config>    
  <modules>
     <Namespace_Checkout>            
        <version>0.1.0</version>        
     </Namespace_Checkout>    
  </modules>        
    <frontend>        
        <routers>            
            <checkout>                
                <args>                    
                    <modules>                        
                        <Namespace_Checkout before="Mage_OneStepCheckout">Namespace_Checkout</Namespace_Checkout>                    
                    </modules>                
                </args>            
            </checkout>        
        </routers>    
    </frontend>     
</config>

应用\代码\本地\命名空间\结帐\控制器\ OnepageController.php

<?php
require_once 'Mage/Checkout/controllers/OnepageController.php';

class Namespace_Checkout_OnepageController extends Mage_Checkout_OnepageController{
    public function successAction(){
        $session = $this->getOnepage()->getCheckout();
        if (!$session->getLastSuccessQuoteId()) {
            $this->_redirect('checkout/cart');
            return;
        }

        $lastQuoteId = $session->getLastQuoteId();
        $lastOrderId = $session->getLastOrderId();
        $lastRecurringProfiles = $session->getLastRecurringProfileIds();
        if (!$lastQuoteId || (!$lastOrderId && empty($lastRecurringProfiles))) {
            $this->_redirect('checkout/cart');
            return;
        }

        $this->customFunction(); // Custom function to call

        $session->clear();
        $this->loadLayout();
        $this->_initLayoutMessages('checkout/session');
        Mage::dispatchEvent('checkout_onepage_controller_success_action', array('order_ids' => array($lastOrderId)));
        $this->renderLayout();
    }

    function customFunction(){
        // This function is calling before clearing order session
        //Here you can put all your custom code 
    }
}
?>

在上面的控制器中,我添加了customFunction()您可以放置​​自定义代码。

希望它会对你有所帮助!

相关问题