Categories: How ToMagento 2

How to add Product to store cart Programmatically in Magento 2

Magento is powerful enough to handle small business as well as large retail stores like Hermes, Paul Smith, Ford, Nike etc. But at behind it takes huge efforts of the store owner to maintain products and run a successful e-commerce store. And we developers are born to drink coffee and to simplify the daily routine task of store owners so they can utilize this time to focus on other factors of the store.
Recently one of our clients was asking us about a script where he wants to add products to cart programmatically to save his time and efforts. Actually, that script is pretty useful to everyone so we decided to share the script on our MageComp blog.

protected $formKey;  
protected $cart;
protected $product;
 
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Data\Form\FormKey $formKey,
\Magento\Checkout\Model\Cart $cart,
\Magento\Catalog\Model\Product $product,
array $data = []) {
 $this->formKey = $formKey;
 $this->cart = $cart;
 $this->product = $product;   
 parent::__construct($context);
}
 
public function execute()
 {
  $productId =10;     // Your Product Id
  $params = array(
             'form_key' => $this->formKey->getFormKey(),
             'product' => $productId,              
             'qty'   =>1     //quantity of product             
         ); 
          
 //Load the product based on productID  
 $_product = $this->product->load($productId);    
 $this->cart->addProduct($_product, $params);
 $this->cart->save();

You can even use or customize this code as per your need to add more additional or custom options as well as configurable product code.
Hope you liked this tiny little solution. Don’t forget to smash that down stars and comment down below if you face any issue while implementing this code.
Happy Coding!

Click to rate this post!
[Total: 21 Average: 4.1]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago