【问题标题】:add bundle products items qty to cart programatically Magento以编程方式将捆绑产品项目数量添加到购物车 Magento
【发布时间】:2015-02-13 07:00:45
【问题描述】:

您好,我需要将捆绑产品及其项目数量添加到购物车编程。为此,我使用以下代码

                $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())

此代码将产品及其所有商品正确添加到购物车中

需要指定每个选项的数量

但它将所有选项 qty 设置为 1

。 你能告诉我我在哪里做错了或者我应该尝试什么。

谢谢

【问题讨论】:

  • 你能提供更多关于你想要达到的目标的细节和例子吗?例如,捆绑中有多少物品?您是否总是将相同数量的每件商品添加到捆绑包中,或者是通过 url 查询参数(上面代码中的$params)传入的数量。捆绑项目是可配置产品、简单产品还是两者兼而有之?如果捆绑的项目是可配置的,那么配置的选项设置在哪里(例如大小)?您只处理一个特定的捆绑包还是需要代码才能处理任何捆绑包?您是否还需要此代码才能用于其他产品类型或仅用于捆绑产品?

标签: magento bundle product cart items


【解决方案1】:

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

$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();

谢谢

【讨论】:

    【解决方案2】:

    "$bundled_qty[$option->getOptionId()] = $cusqtyarray[$cusarraykey];" 你是这样修的吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2015-01-31
      • 1970-01-01
      相关资源
      最近更新 更多