Hello Guys! 👋
In this blog, we add simple products to the cart using Magento 2 root scripts.
first, you need to add the following file to your Magento 2 root directory.
Step 1: add add-to-cart.php file in your magento 2 root.
<?php
use Magento\Framework\AppInterface;
try {
require_once __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
echo 'Autoload error: ' . $e->getMessage();
exit(1);
}
try{
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$formKey = $objectManager->get('Magento\Framework\Data\Form\FormKey');
$cart = $objectManager->get('Magento\Checkout\Model\Cart');
$product = $objectManager->get('Magento\Catalog\Model\Product');
$productId = 3;
$params = array(
'form_key' => $formKey->getFormKey(),
'product' => $productId, //product Id
'qty' =>1 //quantity of product
);
//Load the product based on productID
$_product = $product->load($productId);
$cart->addProduct($_product, $params);
$cart->save();
}
catch(\Exception $e){
print_r($e->getMessage());
}
Step 2: Now run add-to-cart.php from Browser
Hope! It will help you
Thank you 😊

interesting for a very long time
thanks, interesting read
Thanks, I’ve been looking for this for a long time
Thanks, +
_________________
интернет казино рейтинг
Appreciate the recommendation. Let me try it out.
Thank You!
Thanks, I’ve been looking for this for a long time