How to Programmatically Add Product to Cart in Magento 2
Sometimes, Magento 2 store admin needs to prefill the shopping cart with a product whenever a user lands to the website. There are many uses of this functionality; for example, the admin may require to add a virtual product to cart whenever a particular product is added to the cart by a customer, the admin may want to give away the free product by default, the admin requires to integrate a custom system or he may require to send visitors directly to the checkout with the product in the cart.
Default Magento 2 doesn’t allow this. So, I’ve come up with a method to programmatically add product to cart in Magento 2.
Method to Programmatically Add Product to Cart in Magento 2:
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 |
<?php namespace Vendor\Extension\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Data\Form\FormKey; use Magento\Checkout\Model\Cart; use Magento\Catalog\Model\Product; class Post 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() { $productId =10; $params = array( 'form_key' => $this->formKey->getFormKey(), 'product' => $productId, 'qty' =>1 ); $product = $this->product->load($productId); $this->cart->addProduct($product, $params); $this->cart->save(); } } |
Run the above code and your task is done in a blink of an eye! Please let me know in the comment section if everything worked as expected. I would be happy to help if you stuck somewhere.
If you think this code saved you time, rate the post with 5 stars ?
Thank You!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
15 Comments
Hi, Would this code be able to add a product to the cart if an order is shipping to a specific US state? If so, how would that code look when set for state?
Hello Leslie,
Using the above code, you can add the product to the cart.
You need to code additional for shipping.
Thank You
Where you assign customer to cart?
Hello Parbat Singh,
The above solution is to assign the product to the cart. You don’t need to assign customer because if the user is currently login, then the product will be assigned to his cart only.
Thanks.
Hello in magento 2.3.5 it does not update cart total any advice?
Hello,
Please refresh the page after the product is added to the cart.
Thanks.
i have try this solution with api to post product to cart but i got below error can you let me know what is the actual issue,due to which i got this error on api response message:
“message”: “Type Error occurred when creating object: %type”,
“parameters”: {
“type”: “Bstgroup\\CustomApi\\Model\\Custom”
},
Hello Rajan,
The issue seems to be with the API.
Thanks.
Can you tell me the full folder structure of above code?
Where should I place this code?
You need to put the code along with your action in the controller.
Hi Sanjay Jethva, How can we check product has been added to cart ?
Hi Ajwad,
Directly go to the cart page to check the added product.
hello but i am not able to update mini-cart on adding product programatically,
Hi Rajat,
Our blog is for adding the product to the cart which results in the product added to the cart page.
but if you want to change in the mini cart, you have to implement the concept of section.xml