以编程方式将捆绑产品项目数量添加到购物车Magento

时间:2015-02-13 07:00:46

标签: magento bundle product cart items

我需要添加一个捆绑产品及其商品数量到购物车programaticaaly。为此,我使用以下代码

                $cart = Mage::getModel('checkout/cart');
                $cart->init();

                $params = $this->getRequest()->getParams();
                $productId =  3801 ;//3857;

                $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);

                if($product->getTypeId() == "bundle"){

                $bundled_items = array();
                $optionCollection = $product->getTypeInstance()->getOptionsCollection();
                $selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
                $options = $optionCollection->appendSelections($selectionCollection);
                $childsku = array('testing','staging');

                foreach($options as $option) {
                    $_selections = $option->getSelections();
                    foreach($_selections as $selection) {
                        //print_r($selection);


                        $bundled_items[$option->getOptionId()][] = $selection->getSelectionId();
                        $bundled_qty[$selection->getSelectionId()][] = 2;
                     }
                }
print_r($bundled_items);
print_r($bundled_qty);
                $params = array('bundle_option' => $bundled_items,
                                 'bundle_option_qty'=>$bundled_qty, 
                                'qty' => 1,'product'=>$productId);
                }

                if (isset($params['qty'])) {
                $filter = new Zend_Filter_LocalizedToNormalized(
                    array('locale' => Mage::app()->getLocale()->getLocaleCode())
                    );
                $params['qty'] = $filter->filter($params['qty']);
                }

                $product = new Mage_Catalog_Model_Product();
                $product->load($productId);

                $cart->addProduct($product, $params);
                $cart->save();

                Mage::dispatchEvent('checkout_cart_add_product_complete',
                array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())

此代码将产品正确地添加到购物车及其所有项目

  

需要sprcify每个选项qty

     

但它将所有选项数量设置为1

。 你可以告诉我我在做错误的地方或我应该尝试的地方。

谢谢

2 个答案:

答案 0 :(得分:0)

好的,我已经通过在脚本中进行一些更改来解决问题

$cart = Mage::getModel('checkout/cart');
                                $cart->init();
                                $bundled_items = array();
                                $optionCollection = $_product->getTypeInstance()->getOptionsCollection();
                                $selectionCollection = $_product->getTypeInstance()->getSelectionsCollection($_product->getTypeInstance()->getOptionsIds());
                                $options = $optionCollection->appendSelections($selectionCollection);
                                foreach($options as $option) {
                                    $_selections = $option->getSelections();
                                    foreach($_selections as $selection) {
                                        foreach($cusarray as $cusarraykey=> $cusarrayvalue) {
                                            if($selection->getSku()== $cusarrayvalue){
                                                print_r($selection->getSku());
                                                echo $selection->getSku()."<br/>";
                                                $bundled_items[$option->getOptionId()][] = $selection->getSelectionId();
                                                $bundled_qty[$option->getOptionId()] = $cusqtyarray[$cusarraykey];
                                                }
                                        }
                                    }
                                }
                                $params = array('bundle_option' => $bundled_items,
                                                 'bundle_option_qty' => $bundled_qty,
                                                'qty' => $proqty,'product'=>$_productId);

                                if (isset($params['qty'])) {
                                        $filter = new Zend_Filter_LocalizedToNormalized(
                                            array('locale' => Mage::app()->getLocale()->getLocaleCode())
                                            );
                                        $params['qty'] = $filter->filter($params['qty']);
                                }
                                $_product = new Mage_Catalog_Model_Product();
                                $_product->load($_productId);
                                $cart->addProduct($_product, $params);
                                $cart->save();

感谢

答案 1 :(得分:0)

“ $ bundled_qty [$ option-> getOptionId()] = $ cusqtyarray [$ cusarraykey];” 这是您固定的方式。