Magento 2.1 - Load Product in External ScriptI created a custom module ,but getting error, not able to figure...

Ideas for 3rd eye abilities

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Patience, young "Padovan"

Filling an area between two curves

Is Social Media Science Fiction?

Typesetting a double Over Dot on top of a symbol

How can I plot a Farey diagram?

What happens when a metallic dragon and a chromatic dragon mate?

Lied on resume at previous job

Can I find out the caloric content of bread by dehydrating it?

Symmetry in quantum mechanics

Information to fellow intern about hiring?

Is ipsum/ipsa/ipse a third person pronoun, or can it serve other functions?

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

Is there a familial term for apples and pears?

Copycat chess is back

Was there ever an axiom rendered a theorem?

How can I fix this gap between bookcases I made?

Creating a loop after a break using Markov Chain in Tikz

Is this relativistic mass?

LWC and complex parameters

What does 'script /dev/null' do?

Can I legally use front facing blue light in the UK?

Is there a name of the flying bionic bird?



Magento 2.1 - Load Product in External Script


I created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Magento2 - create new/update category programmatically for specific storeView not workingPrice not getting updated for second product…Magento 2.1 Create a filter in the product grid by new attributeMagentoVaultApiDataPaymentTokenInterfaceFactory throws an exception during CheckoutList Products of CategoryMagento 2 : Custom module plugin is not workingError on editing categories in Magento 2Form is not displayed on panel admin Magento 2Magento 2 load external script in my custom page






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







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 = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MagentoApp');
$bootstrap->run($app);
return $app->getObjectManager();


Here's the MagentoApp code:



class MagentoApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
{
protected $objectManager;

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

public function catchException(MagentoFrameworkAppBootstrap $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('MagentoCatalogModelProductRepository')
->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.










share|improve this question































    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 = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
    $app = $bootstrap->createApplication('MagentoApp');
    $bootstrap->run($app);
    return $app->getObjectManager();


    Here's the MagentoApp code:



    class MagentoApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
    {
    protected $objectManager;

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

    public function catchException(MagentoFrameworkAppBootstrap $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('MagentoCatalogModelProductRepository')
    ->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.










    share|improve this question



























      0












      0








      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 = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
      $app = $bootstrap->createApplication('MagentoApp');
      $bootstrap->run($app);
      return $app->getObjectManager();


      Here's the MagentoApp code:



      class MagentoApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
      {
      protected $objectManager;

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

      public function catchException(MagentoFrameworkAppBootstrap $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('MagentoCatalogModelProductRepository')
      ->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.










      share|improve this question
















      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 = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
      $app = $bootstrap->createApplication('MagentoApp');
      $bootstrap->run($app);
      return $app->getObjectManager();


      Here's the MagentoApp code:



      class MagentoApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
      {
      protected $objectManager;

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

      public function catchException(MagentoFrameworkAppBootstrap $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('MagentoCatalogModelProductRepository')
      ->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.







      magento-2.1 external






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 33 mins ago









      Muhammad Anas

      459215




      459215










      asked May 8 '17 at 21:29









      chantronchantron

      84




      84






















          1 Answer
          1






          active

          oldest

          votes


















          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 = MagentoFrameworkAppBootstrap::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 MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
          {
          protected $productId;

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

          public function catchException(MagentoFrameworkAppBootstrap $bootstrap, Exception $exception)
          {
          return false;
          }

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

          $product = $this->_objectManager
          ->get('MagentoCatalogModelProductRepository')
          ->getById($id);

          $store = $this->_objectManager->get('MagentoStoreModelStoreManagerInterface')->getStore();
          $imageUrl = $store
          ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) .
          'catalog/product' .
          $product->getImage();

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





          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f173521%2fmagento-2-1-load-product-in-external-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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 = MagentoFrameworkAppBootstrap::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 MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
            {
            protected $productId;

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

            public function catchException(MagentoFrameworkAppBootstrap $bootstrap, Exception $exception)
            {
            return false;
            }

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

            $product = $this->_objectManager
            ->get('MagentoCatalogModelProductRepository')
            ->getById($id);

            $store = $this->_objectManager->get('MagentoStoreModelStoreManagerInterface')->getStore();
            $imageUrl = $store
            ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) .
            'catalog/product' .
            $product->getImage();

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





            share|improve this answer




























              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 = MagentoFrameworkAppBootstrap::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 MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
              {
              protected $productId;

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

              public function catchException(MagentoFrameworkAppBootstrap $bootstrap, Exception $exception)
              {
              return false;
              }

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

              $product = $this->_objectManager
              ->get('MagentoCatalogModelProductRepository')
              ->getById($id);

              $store = $this->_objectManager->get('MagentoStoreModelStoreManagerInterface')->getStore();
              $imageUrl = $store
              ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) .
              'catalog/product' .
              $product->getImage();

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





              share|improve this answer


























                0












                0








                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 = MagentoFrameworkAppBootstrap::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 MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
                {
                protected $productId;

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

                public function catchException(MagentoFrameworkAppBootstrap $bootstrap, Exception $exception)
                {
                return false;
                }

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

                $product = $this->_objectManager
                ->get('MagentoCatalogModelProductRepository')
                ->getById($id);

                $store = $this->_objectManager->get('MagentoStoreModelStoreManagerInterface')->getStore();
                $imageUrl = $store
                ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) .
                'catalog/product' .
                $product->getImage();

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





                share|improve this answer













                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 = MagentoFrameworkAppBootstrap::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 MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
                {
                protected $productId;

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

                public function catchException(MagentoFrameworkAppBootstrap $bootstrap, Exception $exception)
                {
                return false;
                }

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

                $product = $this->_objectManager
                ->get('MagentoCatalogModelProductRepository')
                ->getById($id);

                $store = $this->_objectManager->get('MagentoStoreModelStoreManagerInterface')->getStore();
                $imageUrl = $store
                ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) .
                'catalog/product' .
                $product->getImage();

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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 9 '17 at 14:54









                chantronchantron

                84




                84






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f173521%2fmagento-2-1-load-product-in-external-script%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                    How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                    變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...