Many times Magento 2 store owners need to add more than 1 product to their store simultaneously in the cart, so that they can be successful in providing an even better experience to the users at the store. Therefore, in this blog, we will tell you about adding multiple products to the cart programmatically on Magento 2 Store.
You can see how to add multiple product to a cart step by step.
Step 1: You need to create custom module in your magento 2.
Here Webiators is our vendor name, and CustomCart is our module name.
Step 2: You need to create routes.xml file in directory path:
app/code/Webiators/CustomCart/etc/frontend and add this code given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" ?> <!-- /** * * @category Webiators * @package Webiators_CustomCart * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route frontName="customcart" id="customcart"> <module name="Webiators_CustomCart"/> </route> </router> </config> |
Step 3: You need to create a controller file Add.php in your module directory path:
app/code/Webiators/CustomCart/Controller/Add and add this code given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php /** * @category Webiators * @package Webiators_CustomCart * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ namespace Webiators\CustomCart\Controller\Add; class Add extends \Magento\Framework\App\Action\Action { protected $_pageFactory; protected $formKey; protected $cart; protected $product; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory, \Magento\Framework\Data\Form\FormKey $formKey, \Magento\Checkout\Model\Cart $cart, \Magento\Catalog\Model\Product $product, array $data = [] ) { $this->_pageFactory = $pageFactory; $this->formKey = $formKey; $this->cart = $cart; $this->product = $product; return parent::__construct($context); } public function execute() { $productIds = $this->_request->getParam('pro_ids'); if (isset($productIds) ) { $productIdsArr = (explode(",",$productIds)); $this->cart->addProductsByIds($productIdsArr); $this->cart->save(); $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('checkout/cart/'); return $resultRedirect; }else{ echo "NO PRODUCT GIVEN"; } } } |
Step 3: Now you need to run all the basic command
1 2 3 4 5 6 7 |
php -dmemory_limit=5G bin/magento setup:upgrade php -dmemory_limit=5G bin/magento setup:di:compile php -dmemory_limit=5G bin/magento setup:static-content:deploy -f php -dmemory_limit=5G bin/magento cache:flush php -dmemory_limit=5G bin/magento cache:clean php -dmemory_limit=5G bin/magento indexer:reindex |
Step 4: Let’s open browser and give the route and controller name and pass the parameters that you want such as https://shop.dev.webiators.com/customcart/add/add/pro_ids/11,12,13
1 |
We hope this post helps you.
Thank You! 😉