I'm porting over a little web service developed by a previous developer that pulled product data from an older Magento 1.9 install and spits it out as a json response.
They bootstrapped Magento 1.9 with this:
$_SERVER['MAGE_IS_DEVELOPER_MODE'] = true;
require_once $_SERVER['DOCUMENT_ROOT'].'/app/Mage.php';
umask(0);
\Mage::app('default');
And they loaded product data with:
$productHelper = \Mage::getModel('catalog/product');
$product = $productHelper->load($product_id);
With Magento 2.1 I'm bootstrapping with:
require getenv('MAGENTO_PATH') . 'app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MagentoApp');
$bootstrap->run($app);
return $app->getObjectManager();
Here's the MagentoApp code:
class MagentoApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
protected $objectManager;
public function launch()
{
$this->_state->setAreaCode('frontend');
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
public function getObjectManager()
{
return $this->_objectManager;
}
}
When I go to get the Magento Product in the little web service's product model, I'm using
$objectManager = $app->getObjectManager();
$product = $objectManager
->get('Magento\Catalog\Model\ProductRepository')
->getById($product_id);
and getting the error Call to a member function get() on null
(referring to the $objectManager
as null) . What's weird is if I exit(var_dump($product->getData())
after that method call, I get the actual product data just fine.
If anyone could shed any light on this, it would be much appreciated.
Thanks very much.