4

在 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_option”和“bundle_option_qty”字段编译数组。循环完成后,我将使用捆绑商品的选项数组将捆绑产品 (ID:833) 添加到购物车。

结果是没有任何东西添加到购物车中。我也对代码进行了一些尝试,但没有成功。

任何人都可以看到我哪里出错了,或者您是否可以将我指向产品选项参数的文档/教程,其中详细说明了 bundle_option 数组(索引是什么,值是什么),这也会有所帮助?

4

1 回答 1

3

我必须检查从前端发送到购物车 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]=17bundle_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不设置数量(所有项目都是 qty:1 添加到捆绑产品中)

编辑 2

事实证明,内置的前端捆绑添加到购物车功能也无法将多个商品添加到购物车!调查这个问题,我发现捆绑数量功能是一个名为 Kabel BundlePlus 的扩展,以前的开发人员可能没有正确安装它,所以我再次下载并重新安装了插件,现在the bundle_option_qty可以在前端和我的插件中使用!

于 2012-09-23T01:12:48.413 回答