- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2017
02:20 PM
07-06-2017
02:20 PM
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
Labels:
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2017
11:40 PM
07-06-2017
11:40 PM
Re: Add Bundle Product to cart from code
you need to pass bundle options array into the post.
Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2017
12:34 AM
07-07-2017
12:34 AM
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.
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.