在Magento中以编程方式将捆绑产品添加到购物车时,我似乎无法在产品选项数组中找到“bundle_option”字段的任何文档 . 所以我不能确定如何正确地做到这一点 .
但这是我的尝试:
$json_obj = json_decode($json_string, true);
//define cart
$cart = Mage::getSingleton('checkout/cart');
$bundle = array();
$bundle_qty = array();
for ($i=0; $i<count($json_obj['basket']['products']); $i++) {
$product_id = int($json_obj['basket']['products'][$i]['id']);
#add individual products to cart
#$product = new Mage_Catalog_Model_Product();
#$product->load($product_id);
#$params = array('product'=>$product_id,'qty'=>1);
#if ($product->getName()) $cart->addProduct($product, $params);
#add products to bundle
$bundle[$i] = $product_id;
if (isset($bundle_qty[$product_id])) $bundle_qty[$product_id] += (int)1;
else $bundle_qty[$product_id] = (int)1;
}
#add to bundled product to cart
$product = new Mage_Catalog_Model_Product();
$product->load(833); #833 = test bundle
$cart->addProduct($product, array('product'=>833,
'qty'=>min(1,int($json_obj['basket']['quantity'])),
'bundle_option'=>$bundle,
'bundle_option_qty'=>$bundle_qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$message = $this->__('Notice: %s item(s) were successfully added to your shopping cart.', $i);
Mage::getSingleton('checkout/session')->addSuccess($message);
}
因此,注释掉的代码是单独添加正常工作的产品 . 现在我正在尝试将产品添加到“Test Bundle”产品中 .
我现在在循环中做的是为'bundle_option'和'bundle_option_qty'字段编译数组 . 循环完成后,我将捆绑产品(ID:833)添加到购物车中,并附带捆绑商品的选项数组 .
结果是没有任何东西被添加到购物车 . 我也玩过一些代码而没有成功 .
任何人都可以看到我出错的地方,或者你是否可以指向我的产品选项参数的doc / tutorial,它详细介绍了bundle_option数组(索引是什么,以及值是什么),这也有帮助吗?
1 回答
我必须检查从前端发送到购物车URL的POST变量,以确定这一点 .
这些是为一个捆绑发布的变量:
从那我发现
bundle_option[1]
在捆绑中引用了 Option 1 . 我还想到了索引bundle_option[1][0]=17
和bundle_option[1][1]=19
的值--17和19指的是selection_id
.分析前端的表单显示了我的selection_id列表 . 我认为一旦在管理>管理产品中更改了捆绑包,选择ID就会改变,所以我使用查找来获取选择ID而不是硬编码 .
我最终得到的代码是这样的:
我希望这能节省很多时间!
编辑
仍然试图解决为什么
bundle_option_qty
没有设置数量(所有项目都是数量:1添加到捆绑产品)编辑2
事实证明,内置的前端捆绑添加到购物车功能也无法向购物车添加多个项目! Looking into the issue I found that the bundle-quantity feature was an extension called Kabel BundlePlus,它可能没有被以前的开发人员正确安装,所以我再次下载并重新安装插件,现在
the bundle_option_qty
正在前端和我的插件中工作!