Magento购物车数量不详

时间:2012-10-30 04:36:27

标签: magento

我有这样的观察员

<events>
 <sales_quote_add_item>
         <observers>
            <reservation_observer_order_save_before1>
                <type>singleton</type>
                <class>Mageec_Total_Model_Observer</class>
                <method>updatePrice</method>
            </reservation_observer_order_save_before1>
         </observers>
 </sales_quote_add_item>
</events>

功能updatePrice如下:

public function updatePrice($observer){

    $event = $observer->getEvent();

    $quote_item = $event->getQuoteItem();


    if ($quote_item->getProductType() == "virtual"){
        $new_price = (float)$quote_item->getProduct()->getPrice();
        $quote_item->setOriginalCustomPrice($new_price);

        $qty = Mage::getSingleton('checkout/cart')->getSummaryQty(); //return 0
                    $qty = $quote_item->getQty(); //return 0 too
        Mage::log(print_r($qty,true));



        $quote_item->setTotalPrice( $new_price * $qty );
    }
}

我无法获得购物车数量或至少报价数量。我认为这是因为这个观察者在购物车对象被填满之前执行。那么在这种情况下我可以在哪里获得数量?

2 个答案:

答案 0 :(得分:1)

我自己找到了解决方案。 当我们将第一个产品放到购物车时,运行sales_quote_add_item观察者。但它的第一个产品和购物车尚未保存。我获得购物车数量的前面的代码返回0.前面的解决方案是在处理订单之前保存购物车。代码示例如下:

public function modifyPrice(Varien_Event_Observer  $observer){
        $cart = Mage::getSingleton('checkout/cart');
        $cart->save();


        $quote_item = $observer->getQuoteItem();
    if ($quote_item->getProductType() == "virtual"){
            $new_price = (float)$quote_item->getProduct()->getPrice();
        $quote_item->setOriginalCustomPrice($new_price);

        $totalItemsInCart = Mage::helper('checkout/cart')->getItemsCount(); //Total items in cart
        Mage::log( "Total Product in Cart : " );  
        Mage::log( print_r($totalItemsInCart, true) );  

        $qty = 1;

        if($totalItemsInCart)   
        {               
            Mage::log( "Total Product in Cart : ".$totalItemsInCart );          
            $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
            Mage::log( "Total Quantity In Cart    : ".(int)$cart['items_qty'] ); 
            $qty = (int)$cart['items_qty'];
        }






        $quote_item->setTotalPrice( $new_price * $qty );
        $quote_item->save();
    }

    return $this;
}

如你所见:

$cart = Mage::getSingleton('checkout/cart');
$cart->save();

这些是解决方案。

答案 1 :(得分:0)

你可以试试这段代码,如果不行,请告诉我

$totalItemsInCart = Mage::helper('checkout/cart')->getItemsCount(); //Total items in cart

if($totalItemsInCart)   
{               
    echo "Total Product in Cart : ".$totalItemsInCart;          
    $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
    echo "Total Quantity In Cart    : ".(int)$cart['items_qty'];    
}