Magento-以编程方式将多种产品添加到购物车 [英] Magento - Adding multiple products to cart programatically

查看:56
本文介绍了Magento-以编程方式将多种产品添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使多个产品添加到购物车的泡菜中,有效地,我有一个带有表单元素的块,该元素定义了添加到购物车的产品(免费产品,由观察者事件处理) 。将产品价格更改为免费的观察员事件运行良好,但是,使用以下方法将多个产品添加到购物车证明很麻烦:

In a bit of a pickle trying to get multiple products to add to cart, effectively I have a block with a form element that defines the products that are added to the cart (free products, which is handled with an observer event). The observer event to change the products price to free is working fine however, adding more than one product to the cart is proving troublesome with the following method:

public function addFreeItems($observer) {
$cart = Mage::getSingleton('checkout/cart');
$freeItems = $_SESSION['package_free_item'];
$freeItemSplit = explode(",", $freeItems);
try {
    foreach ($freeItemSplit as $product) {
        $product = Mage::getModel('catalog/product')->load($product);
        $cart->addProduct($product, array('qty' => '1'));
        $cart->save();
    }
} catch(Exception $e) {
       Mage::log($e->getMessage());
       echo $e->getMessage();
    }
} 

该方法适用于单个项目,并且可以很好地添加,但是后续项(已在数组中的位置[1]明确定义)不会添加到购物车中。

The method works for a single item and adds fine, however the subsequent item (which is definately defined in the array at position [1]) doesn't add to the cart.

我不知道为什么这在技术上不起作用。添加过程中没有异常,调试也显示该数组填充了两个项目。

I'm at a loss as to why this doesnt work as technically it should. No exceptions are being caught in the adding process, and debugging also shows the array as populated with two items.

任何人都可以就为什么这样做不起作用提供任何信息

Can anyone give any light as to why this isn't working?

谢谢!

XML更新:

<sales_quote_add_item>
        <observers>
            <priceupdate_observer>
                <type>singleton</type>
                <class>Edge_Package_Model_ObserverPrice</class>
                <method>updatePrice</method>
            </priceupdate_observer>
        </observers>
</sales_quote_add_item>

有效地,它更新了包裹的价格,但也从其中调用了免费添加的产品。

Effectively it updates the pricing of a package, but also calling the add free products from within it.

编辑2:

public function addFreeItems($observer) {
$route = Mage::app()->getFrontController()->getRequest()->getRouteName();
if($route == "packages" && $_SESSION['package_free_item'] != null ) {
    $freeItems = $_SESSION['package_free_item'];
    $product_ids = explode(",", $freeItems);
    $cart = Mage::getSingleton('checkout/cart');
        foreach ($product_ids as $product_id) {
        $product = Mage::getModel('catalog/product')->load($product_id);
        $cart->addProduct($product, array('qty' => '1', 'product_id' => $product->getId()));  
        }
    $cart->save();
    }
}


推荐答案

<checkout_cart_product_add_after>
        <observers>
            <priceupdate_observer>
                <type>singleton</type>
                <class>Edge_Package_Model_ObserverPrice</class>
                <method>updatePrice</method>
            </priceupdate_observer>
        </observers>
</checkout_cart_product_add_after>

public function addFreeItems($observer) {
   $quote = Mage::getSingleton('checkout/session')->getQuote();
   //foreach loop
   $quote->addProduct($product, array('qty' => '1', 'product_id' => $product->getId()));
}

请参见/ app / code / core / Mage / Checkout / Model中的方法addProduct /Cart.php

see method addProduct in /app/code/core/Mage/Checkout/Model/Cart.php

请参见 http:// magentocommerce .com / boards / viewthread / 39334

这篇关于Magento-以编程方式将多种产品添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆