{"id":163,"date":"2018-08-24T11:15:09","date_gmt":"2018-08-24T11:15:09","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/08\/24\/programmatically-add-product-to-cart-in-magento-2\/"},"modified":"2025-05-21T17:58:38","modified_gmt":"2025-05-21T12:28:38","slug":"programmatically-add-product-to-cart-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-add-product-to-cart-in-magento-2\/","title":{"rendered":"How to Programmatically Add Product to Cart in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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&nbsp;may require&nbsp;to send visitors directly to the checkout with the product in the cart.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Default Magento 2 doesn\u2019t allow this. So, I\u2019ve come up with a method to&nbsp;programmatically add product to cart in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Programmatically Add Product to Cart in Magento 2<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Controller\\Index;\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\Data\\Form\\FormKey;\nuse Magento\\Checkout\\Model\\Cart;\nuse Magento\\Catalog\\Model\\Product;\nclass Post extends Action\n{\n    protected $formKey;   \n    protected $cart;\n    protected $product;\n    public function __construct(\n        Context $context,\n        FormKey $formKey,\n        Cart $cart,\n        Product $product) {\n            $this->formKey = $formKey;\n            $this->cart = $cart;\n            $this->product = $product;      \n            parent::__construct($context);\n    }\n    public function execute()\n     { \n        $productId =10;\n        $params = array(\n                    'form_key' => $this->formKey->getFormKey(),\n                    'product' => $productId, \n                    'qty'   =>1\n                );              \n        $product = $this->product->load($productId);       \n        $this->cart->addProduct($product, $params);\n        $this->cart->save();\n     }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run the above code and your task is done in a blink of an eye!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also try to<a href=\"https:\/\/meetanshi.com\/blog\/how-to-add-sticky-add-to-cart-to-magento-2\/\"> manually add sticky add to cart in Magento<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":14018,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/163\/revisions\/14018"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}