在 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);
因此,注释掉的代码正在单独添加产品,这可以正常工作。现在,我正在尝试将产品添加到“测试 bundle ”产品中。
我现在在循环中所做的是编译“bundle_option”和“bundle_option_qty”字段的数组。循环完成后,我将捆绑产品 (ID:833) 添加到购物车中,其中包含捆绑商品的选项数组。
结果是什么都没有添加到购物车。我也试过代码但没有成功。
任何人都可以看到我哪里出错了,或者如果您可以指点我详细说明 bundle_option 数组(索引是什么,值是什么)的产品选项参数的文档/教程,这也有帮助吗?
最佳答案
我必须检查从前端发送到购物车 URL 的 POST 变量才能弄清楚这一点。
这些是为一个包发布的变量:
bundle_option[1][] 17
bundle_option[1][] 19
bundle_option_qty[1][17] 1
bundle_option_qty[1][19] 1
product 833
qty 2
related_product
由此我发现 bundle_option[1]
指的是 bundle 中的Option 1。
我还发现索引 bundle_option[1][0]=17
和 bundle_option[1][1]=19
的值 - 17 和 19 指的是 selection_id
.
分析前端的表单显示了我的 selection_id 列表。我认为一旦在“管理”>“管理产品”中更改了 bundle ,选择 ID 就会发生变化,因此我使用查找来获取选择 ID,而不是将它们硬编码。
我最终得到的代码是这样的:
$json_string = isset($_POST["json"])? $_POST["json"] : null;
if (!is_null($json_string)) {
$json_obj = json_decode($json_string, true);
#define cart
$cart = Mage::getSingleton('checkout/cart');
#look-up bundle selection ids
$bundled_product = new Mage_Catalog_Model_Product();
$bundled_product->load(833); #833 = test bundle
$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
$bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
);
$bundled_items = array();
foreach ($selectionCollection as $option) {
$bundled_items[$option->product_id] = $option->selection_id;
}
#get bundle items, quantities
$bundle = array();
$bundle_qty = array();
for ($i=0; $i<count($json_obj['basket']['products']); $i++) {
$product_id = (int)$json_obj['basket']['products'][$i]['id'];
$selection_id = $bundled_items[$product_id];
if(!in_array($selection_id,$bundle)) array_push($bundle,$selection_id);
if (isset($bundle_qty[$selection_id])) $bundle_qty[$selection_id] += (int)1;
else $bundle_qty[$selection_id] = (int)1;
}
#add to bundled product to cart
$options = array('product'=>833,
'related_product'=>null,
'bundle_option'=>array(1=>$bundle),
'bundle_option_qty'=>array(1=>$bundle_qty),
'qty'=>(int)$json_obj['basket']['quantity']
);
$cart->addProduct($bundled_product, $options);
$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);
}
我希望这可以节省很多人的时间!
编辑
仍在尝试解决为什么 bundle_option_qty
没有设置数量(所有项目都是 qty:1 添加到捆绑产品中)
编辑2
事实证明,内置的前端捆绑添加到购物车功能也无法将多件商品添加到购物车! Looking into the issue I found that the bundle-quantity feature was an extension called Kabel BundlePlus ,它可能没有被以前的开发人员正确安装,所以我再次下载并重新安装了插件,现在 bundle_option_qty
在前端和我的插件中都可以工作了!
关于php - Magento 将带有 'bundle_option' 的捆绑产品添加到购物车?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12545970/