CERVEAU TECHNOLOGIES IT SOLUTION logo
magento blog

Execute Magento 2 Script in Root Directory

Using below code snippet you can execute any magento 2 script in custom php file on root directory. so create a custom php file like custom.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();

Leave a Comment

Your email address will not be published. Required fields are marked *