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: 20 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

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

2 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

5 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

6 days ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

7 days ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

7 days ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

1 week ago