I am trying to add multiple product to cart at the same time.
if i try to add 3 item same time it add first item with right price rest will added with 0 price.
Here is my code
<?php namespace Kite\Boxes\Controller\Category; class Addpack extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Checkout\Model\Cart */ protected $cart; /** * @var \Magento\Catalog\Model\Product */ protected $productFactory; protected $resultPageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Catalog\Model\ProductFactory $productFactory, \Magento\Checkout\Model\Cart $cart ) { $this->resultPageFactory = $resultPageFactory; $this->cart = $cart; $this->productFactory = $productFactory; parent::__construct($context); } public function execute() { try { $postedData = $this->getRequest()->getPostValue(); //echo "";print_r($postedData['multi_add']);exit; foreach ($postedData['multi_add'] as $productId => $qty) { if($qty>0){ $params = array(); $params['qty'] = $qty;//product quantity $_product = $this->productFactory->create()->load($productId); if ($_product) { $this->cart->addProduct($_product, $params); $this->cart->save(); } } } $this->messageManager->addSuccess(__('Add to cart successfully.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addException( $e, __('%1', $e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addException($e, __('error.')); } /*cart page*/ $this->getResponse()->setRedirect('/checkout/cart/index'); } }
Solved! Go to Solution.
You need to keep $this->cart->save() function outside the loop to correct add a multiple products.
Final code,
<?php namespace Kite\Boxes\Controller\Category; class Addpack extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Checkout\Model\Cart */ protected $cart; /** * @var \Magento\Catalog\Model\Product */ protected $productFactory; protected $resultPageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Catalog\Model\ProductFactory $productFactory, \Magento\Checkout\Model\Cart $cart ) { $this->resultPageFactory = $resultPageFactory; $this->cart = $cart; $this->productFactory = $productFactory; parent::__construct($context); } public function execute() { try { $postedData = $this->getRequest()->getPostValue(); //echo "";print_r($postedData['multi_add']);exit; foreach ($postedData['multi_add'] as $productId => $qty) { if($qty>0){ $params = array(); $params['qty'] = $qty;//product quantity $_product = $this->productFactory->create()->load($productId); if ($_product) { $this->cart->addProduct($_product, $params); } } } /* keep this outside the loop */ $this->cart->save(); $this->messageManager->addSuccess(__('Add to cart successfully.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addException( $e, __('%1', $e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addException($e, __('error.')); } /*cart page*/ $this->getResponse()->setRedirect('/checkout/cart/index'); } }
Here is Working code
<?php namespace Kite\Boxes\Controller\Category; class Addpack extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Framework\View\Result\PageFactory */ protected $resultPageFactory; /** * @var \Magento\Checkout\Model\Session */ protected $checkoutSession; /** * @var \Magento\Quote\Api\CartRepositoryInterface */ protected $cartRepository; /** * @var \Magento\Catalog\Model\ProductFactory */ protected $productFactory; /** * Addpack constructor. * * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory * @param \Magento\Checkout\Model\Session $checkoutSession * @param \Magento\Quote\Api\CartRepositoryInterface $cartRepository * @param \Magento\Catalog\Model\ProductFactory $productFactory */ public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Quote\Api\CartRepositoryInterface $cartRepository, \Magento\Catalog\Model\ProductFactory $productFactory ) { $this->resultPageFactory = $resultPageFactory; $this->checkoutSession = $checkoutSession; $this->cartRepository = $cartRepository; $this->productFactory = $productFactory; parent::__construct($context); } public function execute() { try { $postedData = $this->getRequest()->getPostValue(); $quote = $this->checkoutSession->getQuote(); //echo "";print_r($postedData['multi_add']);exit; foreach ($postedData['multi_add'] as $productId => $qty) { if($qty>0){ $params = array(); $params['qty'] = $qty;//product quantity $product = $this->productFactory->create()->load($productId); if ($product->getId()) { $quote->addProduct( $product, intval($qty) ); } } } $this->cartRepository->save($quote); $this->checkoutSession->replaceQuote($quote)->unsLastRealOrderId(); $this->messageManager->addSuccess(__('Add to cart successfully.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addException( $e, __('%1', $e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addException($e, __('error.')); } /*cart page*/ $this->getResponse()->setRedirect('/checkout/cart/index'); } }
also add following code in your module
etc/frontend/sections.xml
so it update minicart as well.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd"> <action name="category/category/addpack"> <section name="cart"/> </action> </config>
category/category/addpack
Here your action name.
You need to keep $this->cart->save() function outside the loop to correct add a multiple products.
Final code,
<?php namespace Kite\Boxes\Controller\Category; class Addpack extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Checkout\Model\Cart */ protected $cart; /** * @var \Magento\Catalog\Model\Product */ protected $productFactory; protected $resultPageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Catalog\Model\ProductFactory $productFactory, \Magento\Checkout\Model\Cart $cart ) { $this->resultPageFactory = $resultPageFactory; $this->cart = $cart; $this->productFactory = $productFactory; parent::__construct($context); } public function execute() { try { $postedData = $this->getRequest()->getPostValue(); //echo "";print_r($postedData['multi_add']);exit; foreach ($postedData['multi_add'] as $productId => $qty) { if($qty>0){ $params = array(); $params['qty'] = $qty;//product quantity $_product = $this->productFactory->create()->load($productId); if ($_product) { $this->cart->addProduct($_product, $params); } } } /* keep this outside the loop */ $this->cart->save(); $this->messageManager->addSuccess(__('Add to cart successfully.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addException( $e, __('%1', $e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addException($e, __('error.')); } /*cart page*/ $this->getResponse()->setRedirect('/checkout/cart/index'); } }
Here is Working code
<?php namespace Kite\Boxes\Controller\Category; class Addpack extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Framework\View\Result\PageFactory */ protected $resultPageFactory; /** * @var \Magento\Checkout\Model\Session */ protected $checkoutSession; /** * @var \Magento\Quote\Api\CartRepositoryInterface */ protected $cartRepository; /** * @var \Magento\Catalog\Model\ProductFactory */ protected $productFactory; /** * Addpack constructor. * * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory * @param \Magento\Checkout\Model\Session $checkoutSession * @param \Magento\Quote\Api\CartRepositoryInterface $cartRepository * @param \Magento\Catalog\Model\ProductFactory $productFactory */ public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Quote\Api\CartRepositoryInterface $cartRepository, \Magento\Catalog\Model\ProductFactory $productFactory ) { $this->resultPageFactory = $resultPageFactory; $this->checkoutSession = $checkoutSession; $this->cartRepository = $cartRepository; $this->productFactory = $productFactory; parent::__construct($context); } public function execute() { try { $postedData = $this->getRequest()->getPostValue(); $quote = $this->checkoutSession->getQuote(); //echo "";print_r($postedData['multi_add']);exit; foreach ($postedData['multi_add'] as $productId => $qty) { if($qty>0){ $params = array(); $params['qty'] = $qty;//product quantity $product = $this->productFactory->create()->load($productId); if ($product->getId()) { $quote->addProduct( $product, intval($qty) ); } } } $this->cartRepository->save($quote); $this->checkoutSession->replaceQuote($quote)->unsLastRealOrderId(); $this->messageManager->addSuccess(__('Add to cart successfully.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addException( $e, __('%1', $e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addException($e, __('error.')); } /*cart page*/ $this->getResponse()->setRedirect('/checkout/cart/index'); } }
also add following code in your module
etc/frontend/sections.xml
so it update minicart as well.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd"> <action name="category/category/addpack"> <section name="cart"/> </action> </config>
category/category/addpack
Here your action name.