4

I am struggling with Bundle Product add to cart programmatically in Magento 2.

Here I need to add all products with custom price and quantity and I want to do this operation in Product list.phtml file.

Can you help me with this, please?

  • add your code. what you tried? – Bojjaiah Nov 22 '16 at 13:22
  • I just follow this article (magento.stackexchange.com/questions/112788/…) but I did not understand what is what in this array ('bundle_option' => [ 13 => 23, 20 => 24, 14 => 25, 16 => [ 0 => 21, 1 => 12, ], ],) – shankar boss Nov 22 '16 at 13:52
  • If you done with this can you explain me the parameters and right process to add a bundle product to cart programmatically ? – shankar boss Nov 22 '16 at 13:54
  • is it working my solutions? – Bojjaiah Nov 24 '16 at 5:43
  • I am trying this solution friend.Once it is working fine then let you know and accept your answer. – shankar boss Nov 24 '16 at 6:23
1

Create a Controller

<?php

namespace Company\Module\Controller\Index;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\Json as ResultJson;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Data\Form\FormKey;
use Magento\Checkout\Model\Cart;

class Addtocart extends Action
{
    /**
     * @var ProductFactory
     */
    private $productFactory;
    /**
     * @var Cart
     */
    private $cart;
    /**
     * @var FormKey
     */
    private $formKey;
    /**
     * @var JsonFactory
     */
    private $resultJsonFactory;

    /**
     * @param Context $context
     * @param FormKey $formKey
     * @param JsonFactory $resultJsonFactory
     * @param Cart $cart
     * @param ProductFactory $productFactory
     */
    public function __construct(
        Context $context,
        FormKey $formKey,
        JsonFactory $resultJsonFactory,
        Cart $cart,
        ProductFactory $productFactory
    ) {
        parent::__construct($context);
        $this->formKey = $formKey;
        $this->productFactory = $productFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->cart = $cart;
    }

    /**
     *
     * @return ResultJson
     */
    public function execute()
    {
        $data = $this->getRequest()->getParams();
        $resultData = [
            'status' => 'success',
            'status_code' => 1,
            'message' => __('Products Added successfully.')
        ];

        $result = $this->resultJsonFactory->create();
        $qty = 1;

        $product = $this->productFactory->create()->load($productId);
        $productsArray = $this->getBundleOptions($product);
        $params = [
            'product' => $productId,
            'bundle_option' => $productsArray,
            'qty' => $qty
        ];

        try {
            /**
             * Add bundle product in cart
             */
            if ($product->getId()) {
                $this->cart->addProduct($product, $params);
                $this->cart->save();
            }
        } catch (\Exception $e) {
            $resultData = [
                'status' => 'fail',
                'status_code' => 0,
                'message' => __('Unable to add the Product. Exception ' . $e->getMessage())
            ];
            return $result->setData($resultData);
        }
        return $result->setData($resultData);
    }

    /**
     * get all the selection products used in bundle product
     * @param $product
     * @return mixed
     */
    private function getBundleOptions(Product $product)
    {
        $selectionCollection = $product->getTypeInstance()
            ->getSelectionsCollection(
                $product->getTypeInstance()->getOptionsIds($product),
                $product
            );
        $bundleOptions = [];
        foreach ($selectionCollection as $selection) {
            $bundleOptions[$selection->getOptionId()][] = $selection->getSelectionId();
        }
        return $bundleOptions;
    }
}
0

Hi you can try like below.

$product = $this->productRepository->getById("29");
$magento = array(21=>"1",22 => "2", 23 => "3");
$par = array('product' => $product->getId(),
             'bundle_options' => array(  1 => $magento),
             'qty' => $qty);
$this->cart->addProduct($_product,$par);
$this->cart->save();

$magento parameters 21,22,23 are child product ids

$par should be below screenshot.

enter image description here

And finally Bundle product should configure the like below screenshot other wise it won't work.

enter image description here

Now run the above code it will add the bundle product to cart.

Feel free to ask if you have any queries.

  • Isn't it 'bundle_option' ? You write 'bundle_options' – Mr.K Aug 7 '17 at 13:28
0

This worked for me (Magento 2.2.6):

$product = $this->_productRepository->getById($id, false, null, true);
$this->_cart->addProduct($product, $data);

where $data is an array as shown below:

$data = [
    'qty' => 1,
    'product' => 45,
    'bundle_option' => [
        1 => 4,
        2 => 8, 
        3 => 2,
        4 => 7,
    ],
    'bundle_option_qty' => [
        1 => 1,
        2 => 4, 
        3 => 2,
        4 => 14,
    ]
];

the keys inside bundle_option and bundle_option_qty are indexes, not some ID's. I reckon it doesn't matter what these keys are as long as they correspond to the keys in the other array.

Items inside bundle_option array are options ID's and the other contains quantities so in this case we add 1 item ID=4, 4 items ID=8 and so on...

0

Please find below code I have used for add to cart bundle product programmatically.

The $this->_product property is an object of \Magento\Catalog\Model\Product

The $this->cartManagementInterface property is an object of \Magento\Quote\Api\CartManagementInterface

The $this->cartRepositoryInterface property is an object of \Magento\Quote\Api\CartRepositoryInterface

$cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart

$quote = $this->cartRepositoryInterface->get($cartId);

$qty = 2;

$items = $orderData->getAllItems();

foreach ($items as $innerItem) {

    if ($innerItem->getProductType() == 'bundle' ){
            $product = $this->_product->load($innerItem->getProductId());
            $buyRequest = $innerItem->getProductOptionByCode('info_buyRequest');
            $buyRequest = new \Magento\Framework\DataObject($buyRequest);
            $buyRequest->setQty($qty);
            $quote->addProduct($product, $buyRequest);
        }
}
0

I have implemented in the following way,

  1. i have add to cart url like this:

    $cartHelper = $this->helper('Magento\Checkout\Helper\Cart');
    $addToCartUrl = $cartHelper->getAddUrl($product);
    
  2. If the the product type is bundle, then get the bundle options as magento requires to add it to cart.

    $productBundleOptions = $product->getProductOptions()['info_buyRequest'];

  3. then add a form with hidden values of bundle options in phtml file like this :

     <form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post">
                    <?php echo $block->getBlockHtml('formkey')?>
                    <div class="btn">
                        <button type="submit" title="Add to Cart" class="action tocart primary">
                            <span>Add to Cart</span>
                        </button>
                    </div>
                    <?php
                        $bundleOptionArray = $productBundleOptions['bundle_option'];
                        foreach ($bundleOptionArray as $bundleOptionArrayKey => $bundleOptionArrayValue) { ?>
                            <input type="hidden" class="bundle-option-<?= $bundleOptionArrayKey ?>  product bundle option" name="bundle_option[<?= $bundleOptionArrayKey ?>]" value="<?= $bundleOptionArrayValue ?>" id="bundle-option-<?= $bundleOptionArrayKey."-".$bundleOptionArrayValue ?>" checked="checked" aria-required="true">
                        <?php } ?>
                </form>
    

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.