Skip to main content

For example, to get a product name by the custom script

Example 1:

Create test.php at the root of Magento var/www/html/magento2/test.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
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();

You can run script test.php by

http://127.0.0.1/magento2/test.php

Example 2:

stepStep 1: create index.php at the root of magentoMagento 2

var/www/htmlmagento2html/magento2/test/index.php

<?php
require __DIR__ . '/../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);

stepStep 2: create customScript.php

/var/www/html/magento2/test/customScript.php

<?php
class customScript
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
        $id = 12;
        $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
        
        echo $_product->getName();

        return $this->_response;
    }
 
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
 
}

Now you can run this custom script by

http://127.0.0.1/magento2/test/

enter image description here

For example, to get product name by the custom script

Example 1:

Create test.php at the root of Magento var/www/html/magento2/test.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
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();

You can run script test.php by

http://127.0.0.1/magento2/test.php

Example 2:

step 1: create index.php at root of magento 2

var/www/htmlmagento2/test/index.php

<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);

step 2: create customScript.php

/var/www/html/magento2/test/customScript.php

<?php
class customScript
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
        $id = 12;
        $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
        
        echo $_product->getName();

        return $this->_response;
    }
 
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
 
}

Now you can run this custom script by

http://127.0.0.1/magento2/test/

enter image description here

For example, to get a product name by the custom script

Example 1:

Create test.php at the root of Magento var/www/html/magento2/test.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
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();

You can run script test.php by

http://127.0.0.1/magento2/test.php

Example 2:

Step 1: create index.php at the root of Magento 2

var/www/html/magento2/test/index.php

<?php
require __DIR__ . '/../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);

Step 2: create customScript.php

/var/www/html/magento2/test/customScript.php

<?php
class customScript
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
        $id = 12;
        $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
        
        echo $_product->getName();

        return $this->_response;
    }
 
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
 
}

Now you can run this custom script by

http://127.0.0.1/magento2/test/

enter image description here

added 820 characters in body
Source Link
Prince Patel
  • 22.9k
  • 10
  • 98
  • 121

For example you can, to get product name inby the custom script by this step

Example 1:

Create test.php at the root of Magento var/www/html/magento2/test.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
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();

You can run script test.php by

http://127.0.0.1/magento2/test.php

Example 2:

step 1: create index.php at root of magento 2

magento2/test/index.php

var/www/htmlmagento2/test/index.php

<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);

step 2: create customScript.php

magento2/test/customScript.php

/var/www/html/magento2/test/customScript.php

<?php
class customScript
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
        $id = 12;
        $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
        
        echo $_product->getName();

        return $this->_response;
    }
 
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
 
}

Now you can run this custom script by

http://10.16.16.196/magento2/test/http://127.0.0.1/magento2/test/

enter image description here

For example you can get product name in custom script by this step

step 1: create index.php at root of magento 2

magento2/test/index.php

<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);

step 2: create customScript.php

magento2/test/customScript.php

<?php
class customScript
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
        $id = 12;
        $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
        
        echo $_product->getName();

        return $this->_response;
    }
 
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
 
}

Now you can run this custom script by

http://10.16.16.196/magento2/test/

enter image description here

For example, to get product name by the custom script

Example 1:

Create test.php at the root of Magento var/www/html/magento2/test.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
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();

You can run script test.php by

http://127.0.0.1/magento2/test.php

Example 2:

step 1: create index.php at root of magento 2

var/www/htmlmagento2/test/index.php

<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);

step 2: create customScript.php

/var/www/html/magento2/test/customScript.php

<?php
class customScript
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
        $id = 12;
        $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
        
        echo $_product->getName();

        return $this->_response;
    }
 
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
 
}

Now you can run this custom script by

http://127.0.0.1/magento2/test/

enter image description here

Source Link
Prince Patel
  • 22.9k
  • 10
  • 98
  • 121

For example you can get product name in custom script by this step

step 1: create index.php at root of magento 2

magento2/test/index.php

<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);

step 2: create customScript.php

magento2/test/customScript.php

<?php
class customScript
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
        $id = 12;
        $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
        
        echo $_product->getName();

        return $this->_response;
    }
 
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
 
}

Now you can run this custom script by

http://10.16.16.196/magento2/test/

enter image description here