Does Magento add a bundle_option product to the cart?

It seems that I can’t find the documentation for the "bundle_option" field in the product options array, adding the software package to Magento to the cart. Therefore, I cannot be sure how to do it right.

But this is my attempt:

$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); 

}

In this way, commentary code adds products that work correctly. Now I am trying to add products to the "Test Bundle" product.

What I'm doing in the loop right now is compiling arrays for the bundle_option and bundle_option_qty fields. Once the cycle is finished, I add the package product (ID: 833) to the basket with an array of options for related items.

As a result, nothing is added to the basket. I also played with the code a bit without success.

Can someone see where I am wrong, or if you can point me to the doc / tutorial of the product parameter parameter, which describes the bundle_option array (which indexes and what are the values) that will also help?

+4
source share
1 answer

I had to check the POST variables sent to the cart url from the frontend to see this.

These were the variables sent for one package:

 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 

From this, I realized that bundle_option[1] refers to Option 1 in the bundle. I also understood that the values ​​of the indices bundle_option[1][0]=17 and bundle_option[1][1]=19 - 17 and 19 refer to selection_id .

Analysis of the form in the interface showed my list selection_id. I decided that the selection identifier will change as soon as the package is changed in Admin> Manage Products, so I used the search to get the selection identifiers, and not their hard coding.

The code I ended up in was as follows:

 $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); } 

Hope this saves you a lot of time!

Edit

Still trying to decide why bundle_option_qty does not set the quantity (all elements: qty: 1 are added to the package product)

Edit 2

It turned out that the built-in function of adding additional items to the basket cannot add several items to the basket. Having studied the problem, I found that the package number function was an extension of Kabel BundlePlus , it was probably installed incorrectly by previous developers, so I downloaded it again and reinstalled the plugin, and now the bundle_option_qty works both in the interface and in my plugin !

+3
source

Source: https://habr.com/ru/post/1435679/


All Articles