按地点顺序以编程方式更新magento购物车

时间:2014-04-22 04:17:26

标签: magento cart

帮助,想法已经结束......我有config.xml:

     <global>
    <models>
        <ordernow>
            <class>My_OrderNow_Model</class>
        </ordernow>
    </models>
    <events>
        <cms_page_render>
            <observers>
                <OrderNowCheckout>
                    <type>model</type>
                    <class>ordernow/observer</class>
                    <method>OrderNowCheckout</method>
                </OrderNowCheckout>
            </observers>
        </cms_page_render>
        <sales_order_place_before>
            <observers>
                <OrderNowChange>
                    <type>model</type>
                    <class>ordernow/observer</class>
                    <method>OrderNowChange</method>
                </OrderNowChange>
            </observers>
        </sales_order_place_before>
    </events>
</global>

和observer.php

<?php
class My_OrderNow_Model_Observer
{
    public function OrderNowCheckout($observer)
    {

foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
     Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
     }
$id = '20'; 
$qty = '1';
$_product = Mage::getModel('catalog/product')->load($id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($_product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
Mage::app()->getFrontController()->getResponse()->setRedirect('onepagecheckout/')->sendResponse();
  mage::log('OrderNowCheckout');
}
    public function OrderNowChange($observer)
    {
$id = '20'; 
$qty = '35'; /
$_product = Mage::getModel('catalog/product')->load($id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($_product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
 mage::log('OrderNowChange');
    }
}

当我下订单时,我的观察者进入我的函数OrderNowChange,但最后的顺序带有1个数量的项目(必须是35个数量)。日志显示我需要的东西,但法师不这样做。 我错了什么?

2 个答案:

答案 0 :(得分:0)

我想,这可能是事件问题。你可以使用

controller_action_layout_render_before_checkout_onepage_saveOrder

而不是sales_order_place_before

答案 1 :(得分:0)

使用sales_order_place_after观察者和

使用以下代码

                    $qty =35;
        $cart = Mage::getModel('checkout/cart');

        $cart->init();
        $cart->addProduct($my_product, array('qty' => $qty));
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

您的代码看起来不错。只需改变观察者。