cancel
Showing results for 
Search instead for 
Did you mean: 

Add Bundle Product to cart from code

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Add Bundle Product to cart from code

Hi everybody,

my goal is show in checkout/cart the bundle product and the child selected.

This is my code, but doesn't work.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$product = $objectManager->get('Magento\Catalog\Model\Product')->load(703);

$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$addToCartUrl = $listBlock->getAddToCartUrl($product);

 


<form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post">
<?php echo $block->getBlockHtml('formkey')?>
<button type="submit"
title="Add to Cart"
class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>

 

Can someone tell me what's wrong?

Thanks

2 REPLIES 2

Re: Add Bundle Product to cart from code

@giannidimanno

 

you need to pass bundle options array into the post.

 

https://magento.stackexchange.com/questions/146745/programatically-how-to-add-bundle-product-to-cart...


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Add Bundle Product to cart from code

hi Sunil Patel,
thanks for your help. I tried you code but I receive i white page.
below my code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$product = $this->productRepository->getById("703");
$magento = array(82=>"103",82 => "112");
$par = array('product' => $product->getId(),'bundle_options' => array( 1
=> $magento),'qty' => $qty);
$this->cart->addProduct($_product,$par);
$this->cart->save();

I don't know why.
Do you know why? thanks a lot.