magento 2

This post describes how to run Magento 2 code in an external file or script. The external file can be inside the Magento root folder or subfolder.

This example considers that the file is present in the Magento 2 root folder. Let us name the file as test-script.php. So, we want to execute some code when we open

Here is the code below.


$state = $obj->get('Magento\Framework\App\State');
$quoteId = 1;
$quote = $obj->get('Magento\Checkout\Model\Session')


$productId = 1;
$product = $obj->get('Magento\Catalog\Model\ProductRepository')