How to run magento-2 script in root with custom php file


Using below code snippet you can run any magento 2 script in custom php file. so create a custom php file like test.php in root folder where magento 2 has installed and load bootstrap class first then your model according to your requirement.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$id = 1;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($id);

echo $product->getName();
echo $product->getShortDescription();
echo $product->getStatus();

Previous
Next Post »