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 http://mysite.com/test-script.php.

Here is the code below.

 

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

print_r($quote->getOrigData());

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

print_r($product->getData());

?>