2012-09-22 40 views
4

当在Magento中以编程方式将捆绑产品添加到购物车时,我似乎无法在产品选项数组中找到“捆绑选项”字段的任何文档。所以我不能确定如何正确地做到这一点。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)添加到带有捆绑项目的options数组的购物车。

结果是没有东西被添加到购物车。我也玩过代码,但没有成功。

任何人都可以看到我要出错的地方,或者如果你能指向我的产品选项参数的doc /教程,详细说明bundle_option数组(索引是什么,以及值是什么) ?

回答

3

我不得不从前端检查发送到购物车网址的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列表。我发现一旦在Admin> Manage Products中修改了包的选择标识会发生变化,所以我使用查找来获取选择标识,而不是对它们进行硬编码。

我最终得到的代码是:

$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,它可能没有被以前的开发者正确安装,所以我再次下载并重新安装插件,现在the bundle_option_qty正在工作在前端和我的插件!

相关问题