This script allows you to call Magento 2 functions run outside Magento 2 setup. You can create any PHP file in Magento 2 root folder and run Magento 2 from there.
This below script loads a product repository and calls details of product id = 1.
<?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $params = $_SERVER; $bootstrap = Bootstrap::create(BP, $params); //Call Object manager $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $productId = 1; $product = $obj->get('Magento\Catalog\Model\ProductRepository') ->getById($productId); print "Product Name : \n"; print $product->getData('name'); print "\nProduct Price : \n"; print $product->getData('price'); ?>
Leave a Reply