4

I'm trying to figure out how to add a bundled product to cart programmatically in Magento 2 with custom options.

In Magento 1 I could achieve it with the following

$params = array(
    'product' => 14,
    'related_product' => null,
    'bundle_option' => array(
        13 => 23,
        20 => 24,
        14 => 25,
        16 => array(
            0 => 21,
            1 => 12,
        ),
    ),
    'options' => array(
        5 => 'Some Test value to a text field',
    ),
    'qty' => 1,
);

$cart = Mage::getSingleton('checkout/cart');

$product = new Mage_Catalog_Model_Product();
$product->load(14);

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

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
2
  • Have you found solutios? May 18, 2016 at 10:29
  • Still working on it. I haven't had a chance to test out Raphael's approach yet.
    – Eirik
    May 18, 2016 at 18:15

3 Answers 3

3

I reckon the right way of doing it would be the following:

First you need to inject the following classes to your class constructor:

protected $_cart;

protected $_productRepository;

public function __construct(
    ...
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Catalog\Model\ProductRepository $productRepository
) {
    $this->_cart = $cart;
    $this->_productRepository = $productRepository;
    ...
}

Then you can use the following code:

$params = [
    'product' => 14,
    'related_product' => null,
    'bundle_option' => [
        13 => 23,
        20 => 24,
        14 => 25,
        16 => [
            0 => 21,
            1 => 12,
        ],
    ],
    'options' => [
        5 => 'Some Test value to a text field',
    ],
    'qty' => 1
];

$product = $this->_productRepository->getById(14);

$this->_cart->addProduct($product, $params);
$this->_cart->save();
7
  • Hi Raphael, what are these 'bundle_option' => [ 13 => 23, 20 => 24, 14 => 25, 16 => [ 0 => 21, 1 => 12, ], ], can you explain them..? Nov 11, 2016 at 6:28
  • is it bundled items id's or what..? Nov 11, 2016 at 6:31
  • can you update the answer like without hard code?
    – Bojjaiah
    Nov 22, 2016 at 13:25
  • can you please tell me what the meaning of below array from bundle options 16 => [ 0 => 21, 1 => 12, ] Aug 29, 2017 at 4:35
  • @Raphael How can i assign custom price for child bundle product ?
    – Himanshu
    Jun 11, 2019 at 8:54
3

Below you can find my code to how i resolve it, you have to use field selection_id and not sub product id (in this case i use only one multiselect option):

// get selection option in a bundle product
$selectionCollection = $_product->getTypeInstance(true)
                       ->getSelectionsCollection(
$_product->getTypeInstance(true)->getOptionsIds($_product),$_product);
// create bundle option
$cont = 0;
$selectionArray = [];
foreach ($selectionCollection as $proselection){                        
            $selectionArray[$cont] = $proselection->getSelectionId();
            $cont++;
        }
// get options ids 
$optionsCollection = $_product->getTypeInstance(true)
            ->getOptionsCollection($_product);

foreach ($optionsCollection as $options) {
       $id_option = $options->getId();
            }             
// generate bundle_option array
$bundle_option = [$id_option => [$selectionArray]];

$params = [
            'product' => $id,
            'bundle_option' => $bundle_option,
            'qty' => 1
          ];                

$this->cart->addProduct($_product, $params);

The bundle_option results like below:

$bundle_option = array (9 =>  // this is option id
                   array (0 => // this is a simple counter
                     array (0 => '42', // 42 is selection_id
                            1 => '43', // 43 is second selection_id
                           ),
                         ),
                 ) 

I hope that i can help you.

0

I would like to suggest a straight forward approach. It works weather you have set your bundle product options require or not required.

    $params = [
            'form_key' => $this->formKey->getFormKey(),
            'product' => $productId,
            'qty' => $productQty
    ];
    //get all the selection products used in bundle product.
    if('bundle' === $_product->getTypeId()){
            $selectionCollection = $_product->getTypeInstance(true)
            ->getSelectionsCollection(
                $_product->getTypeInstance(true)->getOptionsIds($_product),
                $_product
            );
            foreach ($selectionCollection as $proselection) {
                $params['bundle_option'][$proselection->getOptionId()] = $proselection->getSelectionId();
            }
    }
    $this->cart->addProduct($_product, $params);
    $this->cart->save();

Your Answer

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

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