0

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.

0

I managed to get it working with this (ignore weird static stuff, I didn't have time to rework that class)

ProductModel.php

...
public static function getDataByID($product_id)
{
    Config::boostrap();
    Config::setProductId($product_id);
    return Config::run();
}

Config.php

    public static function boostrap()
    {
        if (!self::$enviroment_loaded) {
            require getenv('MAGENTO_PATH') . 'app/bootstrap.php';
            self::$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
            self::$app = self::$bootstrap->createApplication('MagentoApp');
        }
    }

    public static function run()
    {
        if (!self::$enviroment_loaded) {
            self::$bootstrap->run(self::$app);
            self::$enviroment_loaded = true;
        }

        return self::$app->getProductById();
    }

    public static function setProductId($id)
    {
        self::$app->setProductId($id);
    }

MagentoApp.php

class MagentoApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
    protected $productId;

    public function launch()
    {
        $this->_state->setAreaCode('frontend');
        return $this->_response;
    }

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

    public function getProductById($id = null)
    {
        if (!$id) {
            $id = $this->getProductId();
        }

        $product = $this->_objectManager
            ->get('Magento\Catalog\Model\ProductRepository')
            ->getById($id);

        $store = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
        $imageUrl = $store
            ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) .
                'catalog/product' .
                $product->getImage();

        return [
            'productID'   => $id,
            'productName' => $product->getName(),
            'link'        => $product->getProductUrl(),
            'image'       => $imageUrl
        ];
    }

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.