I would like to add a bundle product to cart programmatically, however i’m experiencing a problem with the bundle_option_qty.
I can change the selection of the bundle option, however the code is somehow not responding to bundle_option_qty. Please find below my code. in this code you can see the definition of $params, in this array I’m setting the bundle_option_qty with 1=>10 and 2 =>20. So I’m expecting the code to add 10 pieces of bundle_option 1871 and 20 pieces of bundle_option 1873, But it is always adding the default quantity as set in the admin section of the bundle product.
< ?php
namespace MageplazaHelloWorldControllerTest;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkDataFormFormKey;
use MagentoCheckoutModelCart;
use MagentoCatalogModelProduct;
class View extends Action
{
protected $formKey;
protected $cart;
protected $product;
public function __construct(
Context $context,
FormKey $formKey,
Cart $cart,
Product $product) {
$this->formKey = $formKey;
$this->cart = $cart;
$this->product = $product;
parent::__construct($context);
}
public function execute()
{
//$aantal = $this->getRequest()->getParam('aantal');
$aantal = 1;
$productId =649;
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product' => $productId,
'bundle_option' => [1=>1871,2=>1873],
'bundle_option_qty' => [1=>10,2=>20],
'qty' =>$aantal,
);
$product = $this->product->load($productId);
$this->cart->addProduct($product, $params);
$this->cart->save();
}
}
Can anybody help me, by showing what i’m doing wrong or give a different solution of how to add a bundle product with bundle option qty.
Thank you in advance.