How to Use code in external file/script in Magento 2
This topic covers an examplethatch uses the existing file in the Magento 2 root folder to show you how to use code in external file/script in Magento 2
This topic covers an example that uses the existing file in the Magento 2 root folder
Let’s begin!
You can set the file’s name to abc.php
, then let’s open domain.com/shell.php to inject the below code:
<?php
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');
$state->setAreaCode('frontend');
$quoteId = 1;
$quote = $obj->get('Magento\Checkout\Model\Session')
->getQuote()
->load($quoteId);
echo '<pre>';
print_r($quote->getOrigData());
echo '</pre>';
$productId = 1;
$product = $obj->get('Magento\Catalog\Model\ProductRepository')
->getById($productId);
echo '<pre>';
print_r($product->getData());
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 while following this tutorial.
Wrap up
That’s all about running code in external file/script in Magento2.
Related Topics
Sam is the CEO & co-founder of Mageplaza, a company established to support Magento merchants with different powerful tools and resources. Sam Nguyen is also the CEO & founder of Avada Commerce, an e-commerce solution provider headquartered in Singapore – aiming to support more than a million online businesses to grow and develop.
- How to create a simple Hello World module for Magento 2
- Magento 2 Block Template Ultimate Guides
- How to Create Module in Magento 2
- How to Create Controller in Magento 2
- How to create CRUD Models in Magento 2
- View: Layouts, Block and Templates
- Configuration - System.xml
- How To Create Admin Menu In Magento 2
- Admin ACL
- Admin Grid
API-GraphQL
Ajax Search
$149
17 reviews
PWA-ready
API-GraphQL
Automatic Related Products
$249
43 reviews
Salesforce CRM Integration
$349
5 reviews
-20%
Sales Booster
$318 ($397)
-20%
Marketing Solution
$278 ($347)
PWA-ready
API-GraphQL
Reward Points
$99
28 reviews
API-GraphQL
Layered Navigation
$149
101 reviews
API-GraphQL
Store Locator
$199
12 reviews
People also searched for
- magento 2 use code external file/script
- 2.3.x, 2.4.x