0

I want to add bundle product with its required options in existing quote in admin,i have added following code to add products to existing quote, its working fine for simple product but bundle products not adding to cart unfortunately, can anyone help me how to achieve this? here is my code:

$qty = 1;
$quote = $this->quoteFactory->create()->load($quote->getId());
$product = $this->productRepository->getById($product->getId());
$quote->addProduct($product, $qty);
$quote->save();
1
  • You need to specify his diffrent options with this one. Oct 2, 2019 at 7:40

1 Answer 1

1

You need to do code like this one.

$qty = 1;
$quote = $this->quoteFactory->create()->load($quote->getId());
$product = $this->productRepository->getById($product->getId());
$magentooptions = array(5=>"1",23=>"2",32=>"3");
$par = array('product' => $product->getId(),
             'bundle_options' => array(1 => $magentooptions),
             'qty' => $qty);
$quote->addProduct($product, $par);
$quote->save();

$magentooptions parameters 5,23,32 are child product ids associated with this main bundle product.

Or you can use this code :

    $quote = $this->quoteFactory->create()->load($quote->getId());
    $product = $this->productRepository->getById($product->getId());
    $par = array('product' => $product->getId(),
            'qty' => 1
    );
    $selectionCollection = $product->getTypeInstance(true)
            ->getSelectionsCollection(
                $product->getTypeInstance(true)->getOptionsIds($product),
                $product
            );
            foreach ($selectionCollection as $proselection) {
                $par['bundle_option'][$proselection->getOptionId()] = $proselection->getSelectionId();
            }

    $quote->addProduct($product, $par);
    $quote->save();
8
  • Hello @dhiren, thanks for reply, what is 1,2 and 3 within 5=>"1",23=>"2",32=>"3" and 'bundle_options' => array(1 => $magentooptions) with this 1 is the key is fixed? can you please tell? and i assume these child product ids must exist in that bundle product Oct 3, 2019 at 5:23
  • Yes you need to replace above id with his real Id of the options. Oct 3, 2019 at 5:28
  • $quote->addProduct($product, $par); this is not working, when i add this code nothing adds to quote but when i use below code it adds only simple products except bundle products: $quote->addProduct($product, $qty); Oct 3, 2019 at 5:33
  • i have actually 3 simple products and 1 bundle products right now so i wanted to add bundle product in quote Oct 3, 2019 at 5:35
  • Yes, but into bundle product, you assign some simple product, which Id you want to pass here, which you want to add. Oct 3, 2019 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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