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.

  • $app = $bootstrap->createApplication('MyClass'); $bootstrap->run($app); And try this link magento.stackexchange.com/questions/39981/… – Mohit Patel Apr 19 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 at 20:57
  • you means load product and attribute using object manager ?? – Mohit Patel Apr 19 at 21:06
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 );
  } 

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.