Magento 2: Application initialization and bootstrap

This process can be used to create cron file for the required operation like insertion and display data of Magento like customer, product, category. We can create file in the root of the website and write the script to process the custom script.

I have created a file called test.php in the root of my Magento instance. In this file, you can write any code of Magento to display or insert data.

<?php

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

	// Change path according to your import file
	require __DIR__ . '/app/bootstrap.php';

	use Magento\Framework\App\Bootstrap;

	$bootstrap = Bootstrap::create(BP, $_SERVER);
	$objectManager = $bootstrap->getObjectManager();

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

	try {   

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

		echo '<pre>';
		print_r($product->getName());
		echo '</pre>';


	} // end of try
	catch(Exception $e) {
		print_r($e->getMessage());
	}

?>