How to Programmatically Add Product to Cart in Magento

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.

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:

That’s it.

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.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 11 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

4 comments On How to Programmatically Add Product to Cart in Magento

  • Thomas Müller-Guttenbrunn

    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

  • Where we can add this code to work?

Leave a reply:

Your email address will not be published.