0

Having a bit of an issue with this one, I managed to get a working interface to Magento by including the following code, which in turn includes the Magento 2 bootstrap.php

use Magento\Framework\App\Bootstrap;
require_once("../app/bootstrap.php");
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
return $obj;

which returns an object that has the callable Magento methods. I've been trying to convert this into a callable class, so that I can call new magento() and have the same functionality, but in more compact code.

use Magento\Framework\App\Bootstrap;
require_once("../app/bootstrap.php");

class magento extends Bootstrap {

    public $params;
    private $bootstrap;
    public $obj;
    private $state;

    public function  __construct() {
        $this->params = $_SERVER;
        $this->bootstrap = Bootstrap::create(BP, $this->params);
        $this->obj = $this->bootstrap->getObjectManager();
        $this->state = $this->obj->get('Magento\Framework\App\State');
        $this->state->setAreaCode('adminhtml');
        return $this->obj;
    }
}

However, when running this class, the subsquent code

$product = $obj->get('Magento\Catalog\Model\ProductRepository')->getById( $entity_id );

the get function is being called from the magento class, not the $object

Fatal error: Uncaught Error: Call to undefined method magento::get()

I think I'm missing something obvious, but can't quite get my head around it.

Duncan Wardlaw
  • 115
  • 1
  • 2
  • 14
  • $app = $bootstrap->createApplication('MyClass'); $bootstrap->run($app); And try this link https://magento.stackexchange.com/questions/39981/%20how-can-i-bootstrap-magento-2-in-a-test-php-script%20/%2040848%20 – Mohit Patel Apr 19 '20 at 20:50
  • adding that to the class produces a fatal error - can you elaborate on how this can be implemented to load a product, attribute options etc? – Duncan Wardlaw Apr 19 '20 at 20:57
  • you means load product and attribute using object manager ?? – Mohit Patel Apr 19 '20 at 21:06

1 Answers1

0

I revised the class after some additional trawling of the internet, this now gives you a quick way of loading the object manager as a resource in any script, that you can in turn load products etc from.

use Magento\Framework\App\Bootstrap;
require_once("../app/bootstrap.php");

class magento extends bootstrap {

    public $params;
    private $bootstrap;
    public $obj;
    private $state;

    public function  __construct() {
        $bootstrap = Bootstrap::create(BP, $_SERVER);
        $this->obj = $bootstrap->getObjectManager();
        $state = $this->obj->get('Magento\Framework\App\State');
        $state->setAreaCode('frontend');
        return $this->obj;
    }

  public function __call( $methodName, $arguments ) {
      return call_user_func_array(array( $this->obj, $methodName ), $arguments );
  } 

}
Duncan Wardlaw
  • 115
  • 1
  • 2
  • 14