0

I want to add bundle products with quantity dynamically into cart. I am able to add products but quantity is not updated. it is always 1. Is there any way to solve this issue.

below code i am using for that.

$product = $this->_productRepository->getById(89,false, null, true);

$bundle_option = [ 1 => array(2,8,18) ];  // 2,8,18 are selection ids, 1 is option id 
$bundle_qty = [ 1 => array(2,3,4) ]; // 2,3,4 are quantity, 1 is option id 

$params = [
    'form_key' => $this->formKey->getFormKey(),
    'product' => 89,
    'bundle_option' => $bundle_option,  // it is working fine 
    'bundle_option_qty' => $bundle_qty,  // logically it will work but in my case it is not working.                
    'qty' => $totalqty
];  

$this->cart->addProduct($product, $params);
$this->cart->save();

Please help. Thanks in advance.

2 Answers 2

0
$product = $this->_productRepository->getById($id, false, null, true);
$this->_cart->addProduct($product, $data);

where $data is an array as shown below:

$data = [
    'qty' => 1,
    'product' => 45,
    'bundle_option' => [
        1 => 4,
        2 => 8, 
        3 => 2,
        4 => 7,
    ],
    'bundle_option_qty' => [
        1 => 1,
        2 => 4, 
        3 => 2,
        4 => 14,
    ]
];

the keys inside bundle_option and bundle_option_qty are indexes, not some ID's. I reckon it doesn't matter what these keys are as long as they correspond to the keys in the other array.

Items inside bundle_option array are options ID's and the other contains quantities so in this case we add 1 item ID=4, 4 items ID=8 and so on...

1
  • $data = [ 'qty' => 1, 'product' => 45, 'bundle_option' => [ 1 => 4, 2 => 8, // this is selection ids 3 => 2, 4 => 7, ], 'bundle_option_qty' => [ 1 => 1, 2 => 4, // this is qty 3 => 2, 4 => 14, ] ]; but it is not working, i already tried. in bundle product, option type is checkbox ( with 5 products selected ) & there is no option e.g User defined quantity [checkbox] ( in magento guide it is mention ) pl. suggest other working solution pl. help me Commented Aug 16, 2019 at 9:36
0

$data = [ 'qty' => 1, 'product' => 45, 'bundle_option' => [ 1 => 4, 2 => 8, // this is selection ids 3 => 2, 4 => 7, ], 'bundle_option_qty' => [ 1 => 1, 2 => 4, // this is qty 3 => 2, 4 => 14, ] ];

but it is not working, i already tried.

in bundle product, option type is checkbox ( with 5 products selected ) & there is no option e.g User defined quantity [checkbox] ( in magento guide it is mention )

pl. suggest other working solution

pl. help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.