Paypal_prepare_line_items的Magento Observer使用paypal和Sub Total Deductions添加新项目

时间:2013-01-22 14:28:04

标签: magento

我创建了一个自定义模块,为客户提供一些折扣费用。 我已经对此link进行了评审,以便为客户费用折扣创建自定义模块。

在这种情况下,当客户拥有自己的折扣点时,他们将在购物车页面中应用折扣点以减少子总计和总计。这些更改在购物车和结帐页等中运行良好。

我的问题是Paypal支付网关项目中的订单子总数和总计未反映。

所以我写了一个事件观察员,用Sub total和添加新行来减去折扣费用到paypal订单项。

以下代码我尝试添加新的折扣费用和子总额扣除

<events>
        <paypal_prepare_line_items>
                <observers>
                    <modulename>
                        <type>singleton</type>
                        <class>modulename/observer</class>
                        <method>prepareItems</method>
                    </modulename>
                </observers>
         </paypal_prepare_line_items>
</events>

观察者核心是

<?php 
class Mycompany_Modulename_Model_Observer extends Mage_Core_Model_Abstract
{
    const TOTAL_FEE = 'Fee';
    const TOTAL_SUBTOTAL = 'subtotal';

    public function prepareItems($observer)
    {   
        $cart = $observer->getPaypalCart();

        if($cart->getSalesEntity()->getFeeAmount())
        {

            $name = 'Custom Discount';
            $qty = '1';
            $amount = -1.00 * $cart->getSalesEntity()->getFeeAmount();
            $identifier = NULL;

            $cart->_shouldRender = true;

            $item = new Varien_Object(array(
                'name'   => $name,
                'qty'    => $qty,
                'amount' => (float)$amount,
            ));

            $cart->_items[] = $item;

            $cart->updateTotal();

        }

        return;

    }

}

任何人都可以指出我正确的解决方案吗?

我需要使用paypal订单项添加折扣费用,并且会正确计算子总数。

任何帮助很多赞赏。

感谢。

2 个答案:

答案 0 :(得分:3)

你可能已经弄明白了。 但你不能做你正在尝试的,我不认为你可以访问_items。而是做:

$cart->addItem($name, $qty, $amount);

我不认为需要调用updateTotal,我已经完成了之前的操作,并且金额被添加到Paypal标准方法的总聚合量中。我没有测试过使用API​​而不是简单形式的其他方法。

答案 1 :(得分:1)

这里是应该解决问题的观察者类代码:

class Mycompany_Modulename_Model_Observer
{
    public function prepareItems(Varien_Event_Observer $observer)
    {        
        $label = SOME_LABEL;
        $qty = SOME_QTY;
        $feeAmount = SOME_FEE_AMOUNT;
        $itemNumber = SOME_IDENTIFIER_OR_SKU;

        /* @var $cart Mage_Paypal_Model_Cart */
        $cart = $observer->getEvent()->getPaypalCart();

        $cart->addItem($label, $qty, $feeAmount, $itemNumber);

        return $this;
    }
}