需要在 magento 中挂钩方法预结帐

Need to hook a method pre checkout in magento

我制作了一个 magento 模块,它有多个方法,其中 1 个方法是 ProcessOrderAction(),我想在每次结帐之前调用这个方法,我的配置文件如下。

<?xml version="1.0"?>
<config>
    <modules>
        <kodework_ongoing>
            <version>0.1.0</version>    <!-- Version number of your module -->
        </kodework_ongoing>
    </modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>kodework_ongoing</module>
                    <frontName>ongoing</frontName>
                </args>
            </mymodule>
        </routers>
  
    </frontend>
 
 

</config> 

您可以使用事件 checkout_cart_save_after 通过观察者挂钩。此事件在购物车保存后触发,通常在购物车项目更改后调用。

要使用它,您需要通过调用 observer class 和您的方法来更新 config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <kodework_ongoing>
            <version>0.1.0</version>    <!-- Version number of your module -->
        </kodework_ongoing>
    </modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>kodework_ongoing</module>
                    <frontName>ongoing</frontName>
                </args>
            </mymodule>
        </routers>
        <!-- Hook into events start -->
        <events>
            <checkout_cart_save_after>
                <observers>
                    <kodework_ongoing>
                        <class>kodework_ongoing/observer</class> <!-- The observer class where with the ProcessOrderAction method -->
                        <method>ProcessOrderAction</method>
                    </kodework_ongoing>
                </observers>
            </checkout_cart_save_after>
        </events>
        <!-- Hook into events end -->
    </frontend>
</config>

如您所见,您需要 observer class app/code/local/Kodework/Ongoing/Model/Observer.php

<?php
class Kodework_Ongoing_Model_Observer
{
    public function ProcessOrderAction($observer)
    {
        $cart = $observer->getData('cart');
        $quote = $cart->getData('quote');
        $items = $quote->getAllVisibleItems();

       // More logic...
    }
}