{"id":512,"date":"2019-07-18T06:57:20","date_gmt":"2019-07-18T06:57:20","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/07\/18\/programmatically-add-product-to-cart-in-magento\/"},"modified":"2025-05-22T16:16:36","modified_gmt":"2025-05-22T10:46:36","slug":"programmatically-add-product-to-cart-in-magento","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-add-product-to-cart-in-magento\/","title":{"rendered":"How to Programmatically Add Product to Cart in Magento"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Earlier, I had given the solution to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/programmatically-add-product-to-cart-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">programmatically add product to cart in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Have you ever thought to surprise your customers&nbsp;with some discounted or&nbsp;FREE&nbsp;stuff? Hell yeah! You may add products to cart automatically when a customer performs a specific action like store visit, reach a specific subtotal, adds a particular product to cart, etc. Also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/set-custom-price-for-products-in-cart-in-magento-2\/\">add product to cart with custom price<\/a>&nbsp;that allows the Magento store owners to create products with custom price in their online stores.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Follow the below code to not only&nbsp;<em><strong>programmatically add product to cart in Magento<\/strong><\/em>, but also update the mini cart automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use this method when, as an admin, you want to offer a free product,&nbsp;add a virtual product or sample product. Customize the code to meet your specific requirements of adding the products to cart automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Programmatically Add Product to Cart in Magento:<\/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=\"\">$prod_id = 41; \/\/ pass product id\n$_product = Mage::getModel('catalog\/product')->load($prod_id);\n$params = array(\n   'product' => $prod_id,\n   'qty' => 1\n);\n\n$cart = Mage::getModel('checkout\/cart');\n$cart->init();\n$cart->addProduct($_product, $params);\n$cart->save();\n\n$quote = Mage::getModel('checkout\/session')->getQuote();\n$quote->collectTotals()->save();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Automate adding products to cart in Magento.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier, I had given the solution to&nbsp;programmatically add product to cart in Magento 2. Have you ever thought to surprise your customers&nbsp;with some discounted or&nbsp;FREE&nbsp;stuff?&#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-512","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/512","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=512"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":15338,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions\/15338"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}