php – Magento使用’bundle_option’将捆绑产品添加到购物车?

在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数组(索引是什么,以及值是什么),这也有帮助吗?

解决方法:

我必须检查从前端发送到购物车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]引用了捆绑中的选项1.
我还想到索引的值bundle_option [1] [0] = 17和bundle_option [1] [1] = 19 – 17和19指的是selection_id.

分析前端的表单显示了我的selection_id列表.我认为一旦在管理>管理产品中更改了捆绑包,选择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没有设置数量(所有项目都是数量:1添加到捆绑产品)

编辑2

事实证明,内置的前端捆绑添加到购物车功能也无法向购物车添加多个项目! Looking into the issue I found that the bundle-quantity feature was an extension called Kabel BundlePlus,它可能没有被以前的开发人员正确安装,所以我再次下载并重新安装插件,现在bundle_option_qty正在前端和我的插件中工作!

相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...