In this digital Era where data is life, no business can survive without collecting and storing data. And all the CMS has its own database where it stores the useful information collected from their customers. Technically, we can collect data in multiple ways using PHP, XML, sessions, cookies etc. But when we want to get or pass data securely and reliably, there is no better option than using JSON. And Magento has wisely implemented JSON to minimize the number of requests & amount of data transmitted between client and server. JSON is nothing but plain text, written with JavaScript object notation which is lightweight & data-interchange format supported by Magento.

Recently working on custom development, we came across the requirement of programmatically adding store products to the shopping cart. Definitely, you have come across such requirement or having thought in mind for adding products to cart at the click of the button. To do the same, we need to create one action which passes the products data & adds products to the shopping cart on click of button. After spending some time in coding, we pop out with a small piece of code that will help to do the same. So, presenting you another blog for programmatically adding products to cart using JSON in Magento 2.

First, we need to execute add to cart functions using custom extension. For that purpose create “Addcart.php” file inside extension folder.
app\code\Vendor\Extension\Controller\Index\Addcart.php

If you want to use static JSON data & Product ID, you can place static values inside the file according to your business need. You can even use & customize this code according to your need of adding different store products to the shopping cart.
That’s it for today, Let us know if you are facing an issue while implementing using this code by commenting below.
Happy Coding!

[Total: 2   Average: 5/5]