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'); ?>
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply