Shopping cart

How to Use code in external file/script in Magento 2

In order to use code in external file/script in Magento 2, the topic covers an example which will use the existing in the Magento 2 root folder. You can set the file’s name to abc.php, then let’s open to inject the below code:

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$quoteId = 1;
$quote = $obj->get('Magento\Checkout\Model\Session')
echo '<pre>';
echo '</pre>';
$productId = 1;
$product = $obj->get('Magento\Catalog\Model\ProductRepository')
echo '<pre>';
echo '</pre>';

When you have done, this means you are successful in using the code in the external file/script in Magento 2. Thanks for your reading and please comment below if there is any trouble in that.

Related Topics

Enjoyed the tutorial? Spread it to your friends!


Brian Tran
Brian Tran
Senior Magento Developer who is passionate about development and love technology

Comments for How to Use code in external file/script in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

You also may like these Magento 2 Extensions

One Step Checkout

120 reviews

Layered Navigation

61 reviews

Gift Card

8 reviews

Reward Points

8 reviews


16 reviews

Shop By Brand

19 reviews

People also searched for:

  • magento 2 use code external file/script