How to Programmatically Add Product to Cart in Magento
Earlier, I had given the solution to programmatically add product to cart in Magento 2.
Have you ever thought to surprise your customers with some discounted or FREE 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 add product to cart with custom price that allows the Magento store owners to create products with custom price in their online stores.
Follow the below code to not only programmatically add product to cart in Magento, but also update the mini cart automatically.
Use this method when, as an admin, you want to offer a free product, add a virtual product or sample product. Customize the code to meet your specific requirements of adding the products to cart automatically.
Steps to Programmatically Add Product to Cart in Magento:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$prod_id = 41; // pass product id $_product = Mage::getModel('catalog/product')->load($prod_id); $params = array( 'product' => $prod_id, 'qty' => 1 ); $cart = Mage::getModel('checkout/cart'); $cart->init(); $cart->addProduct($_product, $params); $cart->save(); $quote = Mage::getModel('checkout/session')->getQuote(); $quote->collectTotals()->save(); |
Automate adding products to cart in Magento.
Do let me know if you have any doubts on the topic in the Comments section below.
Thanks.
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.
6 Comments
Hello,
Thanks for your useful code. I need a code like it, but my Magento is 2, I can not convert this code to new version. Could you help me to modify in version 2?
Best Regards
Hey Jamal, I would like you to have a look at the following given reference blogs,
those are gonna surly help you convert the code to the new version,
Reference :
https://meetanshi.com/blog/programmatically-add-product-to-cart-in-magento-2/
https://meetanshi.com/blog/add-product-to-cart-with-custom-options-in-magento-2/
https://meetanshi.com/blog/add-magento-2-configurable-products-to-cart/
Good afternoon,
Man Name is Thomas and I tried exactly your small codepiece added to cart.phtml (Magento 1.9.3.2 ) for an addon product (depends on storeid).
Product is added but price and subtotal for the product is zero. Also the grandtotal is without the addon product.
After pressing cart update the grandtotal is correct but the product prices remain zero.
I have tried al lot of different code examples, but none is working.
I’m a bit despeted becaus I spent so much hours and nothing is functioning.
Kind regards
Thomas
PS: partly working for lettner.com
Hello,
Please mention the product type that you are adding to the cart.
Thank You.
Where we can add this code to work?
Hi, You can add this controller, model, and block in any of your files.
Thank you.