How to load product by SKU in magento 2Magento 2.1.1 How to load Order with Increment ID using...

Proof by Induction - New to proofs

Should I choose Itemized or Standard deduction?

Where is this triangular-shaped space station from?

For Loop and Sum

Yeshiva University RIETS Semicha Yorei and Yadin

Finding an integral using a table?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

How should I state my MS degree in my CV when it was in practice a joint-program?

Avoiding morning and evening handshakes

How to mitigate "bandwagon attacking" from players?

Predict mars robot position

Do my Windows system binaries contain sensitive information?

What is the purpose of easy combat scenarios that don't need resource expenditure?

Am I a Rude Number?

How to add multiple differently colored borders around a node?

How to push a box with physics engine by another object?

Why is working on the same position for more than 15 years not a red flag?

Dilemma of explaining to interviewer that he is the reason for declining second interview

What can I substitute for soda pop in a sweet pork recipe?

How to satisfy a player character's curiosity about another player character?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

How do Japanese speakers determine the implied topic when none has been mentioned?

How can I improve my fireworks photography?

Is there a way to help users from having to clicking emails twice before logging into a new sandbox



How to load product by SKU in magento 2


Magento 2.1.1 How to load Order with Increment ID using OrderRepository objectIs there ever a reason to prefer $model->load() over service contracts?Magento 2 getMediaGalleryImages() return nullmagento 2 get child product image thumbnailHow to Get product id and Sku By product name magento 2..?Why Can't I Load a Product by SKU using loadBySku()?Error on Mage::getModel('catalog/product')->load($sku, 'sku');Product updates via XML-RPC API not taking effectHow to set custom created attribute value for productMagento2: How to load product by idMagento - $product->loadByAttribute('sku', $sku) not workingMagento 2.1 Load Product By ID and Change SKU ProgrammaticallyLoad product stock quantity by SKU : Magento 2Get custom Product Atributes from each products at rootMagento 2: How to call block/model method on a new custom checkout page?













22















It's pretty basic, but I can’t find a working example on Stackexchange or google. I want to load a product from a helper or block. I already tried some things like:



$objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

$product = $objectManager->create('MagentoCatalogApiDataProductInterface');

$product->get('<SKU>');

$product->getName();


This returns nothing. I also tried loading any available models and API’s, but nothing seems to work with SKU’s.










share|improve this question



























    22















    It's pretty basic, but I can’t find a working example on Stackexchange or google. I want to load a product from a helper or block. I already tried some things like:



    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

    $product = $objectManager->create('MagentoCatalogApiDataProductInterface');

    $product->get('<SKU>');

    $product->getName();


    This returns nothing. I also tried loading any available models and API’s, but nothing seems to work with SKU’s.










    share|improve this question

























      22












      22








      22


      2






      It's pretty basic, but I can’t find a working example on Stackexchange or google. I want to load a product from a helper or block. I already tried some things like:



      $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

      $product = $objectManager->create('MagentoCatalogApiDataProductInterface');

      $product->get('<SKU>');

      $product->getName();


      This returns nothing. I also tried loading any available models and API’s, but nothing seems to work with SKU’s.










      share|improve this question














      It's pretty basic, but I can’t find a working example on Stackexchange or google. I want to load a product from a helper or block. I already tried some things like:



      $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

      $product = $objectManager->create('MagentoCatalogApiDataProductInterface');

      $product->get('<SKU>');

      $product->getName();


      This returns nothing. I also tried loading any available models and API’s, but nothing seems to work with SKU’s.







      magento2 product






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 17 '16 at 20:07









      Dennis van SchaikDennis van Schaik

      5031318




      5031318






















          7 Answers
          7






          active

          oldest

          votes


















          40














          The correct way, according to Magento 2 service contracts, is using repositories:



          $product = $this->productRepositoryInterface->get($sku);


          Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.



          Full example:



          ...
          private $productRepository;
          ...
          public function __construct(
          ...
          MagentoCatalogApiProductRepositoryInterface $productRepository
          ...
          ) {
          ...
          $this->productRepository = $productRepository;
          ...
          }

          public function loadMyProduct($sku)
          {
          return $this->productRepository->get($sku);
          }
          ...


          Note:



          If the product does not exists, this method triggers a NoSuchEntityException error as it would be in the best Magento2 practice.



          So, if you need to handle somehow, wrap it in a try/catch block.






          share|improve this answer





















          • 9





            This method gives an error when no product is found. So for checking if an product exists, It seems @fschmengler's solution might be the better way to go.

            – Dennis van Schaik
            Apr 20 '16 at 11:00






          • 1





            can you pls add more details about Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.? What shall I do? Thanks a lot

            – davideghz
            Dec 16 '16 at 17:17











          • See my updated post

            – Phoenix128_RiccardoT
            Dec 16 '16 at 17:25











          • The actual proper Magento 2 way to use service contracts is more complicated. Repository->get() is still a model call which shouldn't be used. The correct way is to use repository->getList() with SearchCriteriaBuilder. See my answer on Orders (pretty much the same method): magento.stackexchange.com/questions/140374/…

            – Jacco Amersfoort
            Apr 10 '17 at 10:08






          • 1





            @JaccoAmersfoort, I confirm "get" is the right way. In your example it has been suggested because "get" in order repository requires "order id" and that user was looking for "increment id". In that case a getList is the best option, but if you can use "get" you should use it.

            – Phoenix128_RiccardoT
            Apr 10 '17 at 10:57



















          22














          Instead of using the object manager directly, inject the ProductFactory:



          public function __construct(MagentoCatalogModelProductFactory $productFactory)
          {
          $this->productFactory = $productFactory;
          }


          Then use it like this:



          $product = $this->productFactory->create();
          $product->loadByAttribute('sku', $sku);


          or to do a full load (the above loads it using a collection):



          $product = $this->productFactory->create();
          $product->load($product->getIdBySku($sku));





          share|improve this answer



















          • 6





            Actually while this is still working, using load() and collections is the "Magento 1" way, better use the repository as suggested by @RiccardoT.

            – Fabian Schmengler
            Apr 17 '16 at 20:46






          • 1





            Also, your method will return a Product model, whilst using the Repository will give you a Product data model (Api/Data/Product), which is a Product model converted into a dumbed-down DTO. Something to consider, as they're quite different.

            – nevvermind
            Apr 18 '16 at 0:02











          • @FabianSchmengler: Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working.

            – Slimshadddyyy
            Jan 31 '18 at 7:24











          • @FabianSchmengler yes recommendation of Repository is good as @RiccardoT answer. but what if i entered wrong sku then it will break whole operation & throw exception, so in this case we must have to depend on productFactory

            – Himanshu
            Nov 5 '18 at 6:20











          • @Himanshu catch the exception. And if you need a fresh product instance in that case, you can still create it using the factory

            – Fabian Schmengler
            Nov 5 '18 at 17:00



















          4














          I like @phoenix128-riccardot 's answer, but would add an exception, just in case the product does not exist:




          try {
          $product = $this->productRepositoryInterface->get($sku);
          } catch (MagentoFrameworkExceptionNoSuchEntityException $e){
          // insert your error handling here
          }


          I was not able to add it as a comment (too low reputation), sorry.






          share|improve this answer































            2














            Try this:



            /** @var MagentoCatalogModelProductFactory $productFactory */
            $product = $productFactory->create();
            $product->loadByAttribute('sku', 'my sku');

            // $product->load($product->getId()); // may need to do this too,
            // see MagentoFrameworkApiAttributeValueFactoryAbstractModel::loadByAttribute





            share|improve this answer
























            • I need to get the product_id from sku (column of csv) during csv import and save only the product_id..How to achieve?

              – Sushivam
              Aug 25 '16 at 9:45



















            2














            You can try that



            $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

            $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');
            $productObj = $productRepository->get('<SKU>');

            echo $productObj->getId();





            share|improve this answer































              0














              Using Dependency Injection (DI)



              Here is the example code to get the product information by product Id and SKU in Magento 2 using dependency injection.



              In this, we might need to inject the object of the MagentoCatalogModelProductRepository class in the constructor of our module’s block class and access it from the view ( .phtml ) file.



              Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php



              <?php
              namespace YourCompanyNameYourModuleNameBlock;

              class YourCustomBlock extends MagentoFrameworkViewElementTemplate
              {
              protected $_productRepository;

              public function __construct(
              MagentoFrameworkViewElementTemplateContext $context,
              MagentoCatalogModelProductRepository $productRepository,
              array $data = []
              ) {
              $this->_productRepository = $productRepository;
              parent::__construct($context, $data);
              }

              public function getProductById($id) {
              return $this->_productRepository->getById($id);
              }

              public function getProductBySku($sku) {
              return $this->_productRepository->get($sku);
              }
              }


              Now, we can use the functions in our view (.phtml) file as follows.



              // get product by id
              $product = $block->getProductById(15);

              // get product by sku
              $product = $block->getProductBySku('MT12');

              echo $product->getEntityId() . '<br>';
              echo $product->getName() . '<br>';
              echo $product->getSKU() . '<br>';
              echo $product->getPrice() . '<br>';
              echo $product->getSpecialPrice() . '<br>';
              echo $product->getTypeId() . '<br>';
              echo $product->getProductUrl() . '<br>';


              Using Object Manager



              Here is the example code to get the product information by product id and SKU in Magento 2 using object manager.



              $objectManager = MagentoFrameworkAppObjectManager::getInstance();

              $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');

              // get product by product id
              $product = $productRepository->getById(15);

              // get product by product sku
              $product = $productRepository->get('MT12');

              echo $product->getEntityId() . '<br>';
              echo $product->getName() . '<br>';
              echo $product->getSKU() . '<br>';
              echo $product->getPrice() . '<br>';
              echo $product->getSpecialPrice() . '<br>';
              echo $product->getTypeId() . '<br>';
              echo $product->getProductUrl() . '<br>';




              share































                -4














                <?php
                use MagentoFrameworkAppBootstrap;
                require __DIR__ . '/app/bootstrap.php';
                $bootstrap = Bootstrap::create(BP, $_SERVER);
                $obj = $bootstrap->getObjectManager();
                $state = $obj->get('MagentoFrameworkAppState');
                $state->setAreaCode('frontend');


                $sku ='24-MB01';
                $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                $productObject = $objectManager->get('MagentoCatalogModelProduct');
                $product = $productObject->loadByAttribute('sku', $sku);
                echo $product->getName();

                ?>





                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%2f111245%2fhow-to-load-product-by-sku-in-magento-2%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  40














                  The correct way, according to Magento 2 service contracts, is using repositories:



                  $product = $this->productRepositoryInterface->get($sku);


                  Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.



                  Full example:



                  ...
                  private $productRepository;
                  ...
                  public function __construct(
                  ...
                  MagentoCatalogApiProductRepositoryInterface $productRepository
                  ...
                  ) {
                  ...
                  $this->productRepository = $productRepository;
                  ...
                  }

                  public function loadMyProduct($sku)
                  {
                  return $this->productRepository->get($sku);
                  }
                  ...


                  Note:



                  If the product does not exists, this method triggers a NoSuchEntityException error as it would be in the best Magento2 practice.



                  So, if you need to handle somehow, wrap it in a try/catch block.






                  share|improve this answer





















                  • 9





                    This method gives an error when no product is found. So for checking if an product exists, It seems @fschmengler's solution might be the better way to go.

                    – Dennis van Schaik
                    Apr 20 '16 at 11:00






                  • 1





                    can you pls add more details about Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.? What shall I do? Thanks a lot

                    – davideghz
                    Dec 16 '16 at 17:17











                  • See my updated post

                    – Phoenix128_RiccardoT
                    Dec 16 '16 at 17:25











                  • The actual proper Magento 2 way to use service contracts is more complicated. Repository->get() is still a model call which shouldn't be used. The correct way is to use repository->getList() with SearchCriteriaBuilder. See my answer on Orders (pretty much the same method): magento.stackexchange.com/questions/140374/…

                    – Jacco Amersfoort
                    Apr 10 '17 at 10:08






                  • 1





                    @JaccoAmersfoort, I confirm "get" is the right way. In your example it has been suggested because "get" in order repository requires "order id" and that user was looking for "increment id". In that case a getList is the best option, but if you can use "get" you should use it.

                    – Phoenix128_RiccardoT
                    Apr 10 '17 at 10:57
















                  40














                  The correct way, according to Magento 2 service contracts, is using repositories:



                  $product = $this->productRepositoryInterface->get($sku);


                  Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.



                  Full example:



                  ...
                  private $productRepository;
                  ...
                  public function __construct(
                  ...
                  MagentoCatalogApiProductRepositoryInterface $productRepository
                  ...
                  ) {
                  ...
                  $this->productRepository = $productRepository;
                  ...
                  }

                  public function loadMyProduct($sku)
                  {
                  return $this->productRepository->get($sku);
                  }
                  ...


                  Note:



                  If the product does not exists, this method triggers a NoSuchEntityException error as it would be in the best Magento2 practice.



                  So, if you need to handle somehow, wrap it in a try/catch block.






                  share|improve this answer





















                  • 9





                    This method gives an error when no product is found. So for checking if an product exists, It seems @fschmengler's solution might be the better way to go.

                    – Dennis van Schaik
                    Apr 20 '16 at 11:00






                  • 1





                    can you pls add more details about Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.? What shall I do? Thanks a lot

                    – davideghz
                    Dec 16 '16 at 17:17











                  • See my updated post

                    – Phoenix128_RiccardoT
                    Dec 16 '16 at 17:25











                  • The actual proper Magento 2 way to use service contracts is more complicated. Repository->get() is still a model call which shouldn't be used. The correct way is to use repository->getList() with SearchCriteriaBuilder. See my answer on Orders (pretty much the same method): magento.stackexchange.com/questions/140374/…

                    – Jacco Amersfoort
                    Apr 10 '17 at 10:08






                  • 1





                    @JaccoAmersfoort, I confirm "get" is the right way. In your example it has been suggested because "get" in order repository requires "order id" and that user was looking for "increment id". In that case a getList is the best option, but if you can use "get" you should use it.

                    – Phoenix128_RiccardoT
                    Apr 10 '17 at 10:57














                  40












                  40








                  40







                  The correct way, according to Magento 2 service contracts, is using repositories:



                  $product = $this->productRepositoryInterface->get($sku);


                  Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.



                  Full example:



                  ...
                  private $productRepository;
                  ...
                  public function __construct(
                  ...
                  MagentoCatalogApiProductRepositoryInterface $productRepository
                  ...
                  ) {
                  ...
                  $this->productRepository = $productRepository;
                  ...
                  }

                  public function loadMyProduct($sku)
                  {
                  return $this->productRepository->get($sku);
                  }
                  ...


                  Note:



                  If the product does not exists, this method triggers a NoSuchEntityException error as it would be in the best Magento2 practice.



                  So, if you need to handle somehow, wrap it in a try/catch block.






                  share|improve this answer















                  The correct way, according to Magento 2 service contracts, is using repositories:



                  $product = $this->productRepositoryInterface->get($sku);


                  Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.



                  Full example:



                  ...
                  private $productRepository;
                  ...
                  public function __construct(
                  ...
                  MagentoCatalogApiProductRepositoryInterface $productRepository
                  ...
                  ) {
                  ...
                  $this->productRepository = $productRepository;
                  ...
                  }

                  public function loadMyProduct($sku)
                  {
                  return $this->productRepository->get($sku);
                  }
                  ...


                  Note:



                  If the product does not exists, this method triggers a NoSuchEntityException error as it would be in the best Magento2 practice.



                  So, if you need to handle somehow, wrap it in a try/catch block.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 5 '18 at 11:16

























                  answered Apr 17 '16 at 20:42









                  Phoenix128_RiccardoTPhoenix128_RiccardoT

                  5,31021230




                  5,31021230








                  • 9





                    This method gives an error when no product is found. So for checking if an product exists, It seems @fschmengler's solution might be the better way to go.

                    – Dennis van Schaik
                    Apr 20 '16 at 11:00






                  • 1





                    can you pls add more details about Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.? What shall I do? Thanks a lot

                    – davideghz
                    Dec 16 '16 at 17:17











                  • See my updated post

                    – Phoenix128_RiccardoT
                    Dec 16 '16 at 17:25











                  • The actual proper Magento 2 way to use service contracts is more complicated. Repository->get() is still a model call which shouldn't be used. The correct way is to use repository->getList() with SearchCriteriaBuilder. See my answer on Orders (pretty much the same method): magento.stackexchange.com/questions/140374/…

                    – Jacco Amersfoort
                    Apr 10 '17 at 10:08






                  • 1





                    @JaccoAmersfoort, I confirm "get" is the right way. In your example it has been suggested because "get" in order repository requires "order id" and that user was looking for "increment id". In that case a getList is the best option, but if you can use "get" you should use it.

                    – Phoenix128_RiccardoT
                    Apr 10 '17 at 10:57














                  • 9





                    This method gives an error when no product is found. So for checking if an product exists, It seems @fschmengler's solution might be the better way to go.

                    – Dennis van Schaik
                    Apr 20 '16 at 11:00






                  • 1





                    can you pls add more details about Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.? What shall I do? Thanks a lot

                    – davideghz
                    Dec 16 '16 at 17:17











                  • See my updated post

                    – Phoenix128_RiccardoT
                    Dec 16 '16 at 17:25











                  • The actual proper Magento 2 way to use service contracts is more complicated. Repository->get() is still a model call which shouldn't be used. The correct way is to use repository->getList() with SearchCriteriaBuilder. See my answer on Orders (pretty much the same method): magento.stackexchange.com/questions/140374/…

                    – Jacco Amersfoort
                    Apr 10 '17 at 10:08






                  • 1





                    @JaccoAmersfoort, I confirm "get" is the right way. In your example it has been suggested because "get" in order repository requires "order id" and that user was looking for "increment id". In that case a getList is the best option, but if you can use "get" you should use it.

                    – Phoenix128_RiccardoT
                    Apr 10 '17 at 10:57








                  9




                  9





                  This method gives an error when no product is found. So for checking if an product exists, It seems @fschmengler's solution might be the better way to go.

                  – Dennis van Schaik
                  Apr 20 '16 at 11:00





                  This method gives an error when no product is found. So for checking if an product exists, It seems @fschmengler's solution might be the better way to go.

                  – Dennis van Schaik
                  Apr 20 '16 at 11:00




                  1




                  1





                  can you pls add more details about Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.? What shall I do? Thanks a lot

                  – davideghz
                  Dec 16 '16 at 17:17





                  can you pls add more details about Use MagentoCatalogApiProductRepositoryInterface to get it in your constructor.? What shall I do? Thanks a lot

                  – davideghz
                  Dec 16 '16 at 17:17













                  See my updated post

                  – Phoenix128_RiccardoT
                  Dec 16 '16 at 17:25





                  See my updated post

                  – Phoenix128_RiccardoT
                  Dec 16 '16 at 17:25













                  The actual proper Magento 2 way to use service contracts is more complicated. Repository->get() is still a model call which shouldn't be used. The correct way is to use repository->getList() with SearchCriteriaBuilder. See my answer on Orders (pretty much the same method): magento.stackexchange.com/questions/140374/…

                  – Jacco Amersfoort
                  Apr 10 '17 at 10:08





                  The actual proper Magento 2 way to use service contracts is more complicated. Repository->get() is still a model call which shouldn't be used. The correct way is to use repository->getList() with SearchCriteriaBuilder. See my answer on Orders (pretty much the same method): magento.stackexchange.com/questions/140374/…

                  – Jacco Amersfoort
                  Apr 10 '17 at 10:08




                  1




                  1





                  @JaccoAmersfoort, I confirm "get" is the right way. In your example it has been suggested because "get" in order repository requires "order id" and that user was looking for "increment id". In that case a getList is the best option, but if you can use "get" you should use it.

                  – Phoenix128_RiccardoT
                  Apr 10 '17 at 10:57





                  @JaccoAmersfoort, I confirm "get" is the right way. In your example it has been suggested because "get" in order repository requires "order id" and that user was looking for "increment id". In that case a getList is the best option, but if you can use "get" you should use it.

                  – Phoenix128_RiccardoT
                  Apr 10 '17 at 10:57













                  22














                  Instead of using the object manager directly, inject the ProductFactory:



                  public function __construct(MagentoCatalogModelProductFactory $productFactory)
                  {
                  $this->productFactory = $productFactory;
                  }


                  Then use it like this:



                  $product = $this->productFactory->create();
                  $product->loadByAttribute('sku', $sku);


                  or to do a full load (the above loads it using a collection):



                  $product = $this->productFactory->create();
                  $product->load($product->getIdBySku($sku));





                  share|improve this answer



















                  • 6





                    Actually while this is still working, using load() and collections is the "Magento 1" way, better use the repository as suggested by @RiccardoT.

                    – Fabian Schmengler
                    Apr 17 '16 at 20:46






                  • 1





                    Also, your method will return a Product model, whilst using the Repository will give you a Product data model (Api/Data/Product), which is a Product model converted into a dumbed-down DTO. Something to consider, as they're quite different.

                    – nevvermind
                    Apr 18 '16 at 0:02











                  • @FabianSchmengler: Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working.

                    – Slimshadddyyy
                    Jan 31 '18 at 7:24











                  • @FabianSchmengler yes recommendation of Repository is good as @RiccardoT answer. but what if i entered wrong sku then it will break whole operation & throw exception, so in this case we must have to depend on productFactory

                    – Himanshu
                    Nov 5 '18 at 6:20











                  • @Himanshu catch the exception. And if you need a fresh product instance in that case, you can still create it using the factory

                    – Fabian Schmengler
                    Nov 5 '18 at 17:00
















                  22














                  Instead of using the object manager directly, inject the ProductFactory:



                  public function __construct(MagentoCatalogModelProductFactory $productFactory)
                  {
                  $this->productFactory = $productFactory;
                  }


                  Then use it like this:



                  $product = $this->productFactory->create();
                  $product->loadByAttribute('sku', $sku);


                  or to do a full load (the above loads it using a collection):



                  $product = $this->productFactory->create();
                  $product->load($product->getIdBySku($sku));





                  share|improve this answer



















                  • 6





                    Actually while this is still working, using load() and collections is the "Magento 1" way, better use the repository as suggested by @RiccardoT.

                    – Fabian Schmengler
                    Apr 17 '16 at 20:46






                  • 1





                    Also, your method will return a Product model, whilst using the Repository will give you a Product data model (Api/Data/Product), which is a Product model converted into a dumbed-down DTO. Something to consider, as they're quite different.

                    – nevvermind
                    Apr 18 '16 at 0:02











                  • @FabianSchmengler: Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working.

                    – Slimshadddyyy
                    Jan 31 '18 at 7:24











                  • @FabianSchmengler yes recommendation of Repository is good as @RiccardoT answer. but what if i entered wrong sku then it will break whole operation & throw exception, so in this case we must have to depend on productFactory

                    – Himanshu
                    Nov 5 '18 at 6:20











                  • @Himanshu catch the exception. And if you need a fresh product instance in that case, you can still create it using the factory

                    – Fabian Schmengler
                    Nov 5 '18 at 17:00














                  22












                  22








                  22







                  Instead of using the object manager directly, inject the ProductFactory:



                  public function __construct(MagentoCatalogModelProductFactory $productFactory)
                  {
                  $this->productFactory = $productFactory;
                  }


                  Then use it like this:



                  $product = $this->productFactory->create();
                  $product->loadByAttribute('sku', $sku);


                  or to do a full load (the above loads it using a collection):



                  $product = $this->productFactory->create();
                  $product->load($product->getIdBySku($sku));





                  share|improve this answer













                  Instead of using the object manager directly, inject the ProductFactory:



                  public function __construct(MagentoCatalogModelProductFactory $productFactory)
                  {
                  $this->productFactory = $productFactory;
                  }


                  Then use it like this:



                  $product = $this->productFactory->create();
                  $product->loadByAttribute('sku', $sku);


                  or to do a full load (the above loads it using a collection):



                  $product = $this->productFactory->create();
                  $product->load($product->getIdBySku($sku));






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 17 '16 at 20:41









                  Fabian SchmenglerFabian Schmengler

                  54.7k21133346




                  54.7k21133346








                  • 6





                    Actually while this is still working, using load() and collections is the "Magento 1" way, better use the repository as suggested by @RiccardoT.

                    – Fabian Schmengler
                    Apr 17 '16 at 20:46






                  • 1





                    Also, your method will return a Product model, whilst using the Repository will give you a Product data model (Api/Data/Product), which is a Product model converted into a dumbed-down DTO. Something to consider, as they're quite different.

                    – nevvermind
                    Apr 18 '16 at 0:02











                  • @FabianSchmengler: Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working.

                    – Slimshadddyyy
                    Jan 31 '18 at 7:24











                  • @FabianSchmengler yes recommendation of Repository is good as @RiccardoT answer. but what if i entered wrong sku then it will break whole operation & throw exception, so in this case we must have to depend on productFactory

                    – Himanshu
                    Nov 5 '18 at 6:20











                  • @Himanshu catch the exception. And if you need a fresh product instance in that case, you can still create it using the factory

                    – Fabian Schmengler
                    Nov 5 '18 at 17:00














                  • 6





                    Actually while this is still working, using load() and collections is the "Magento 1" way, better use the repository as suggested by @RiccardoT.

                    – Fabian Schmengler
                    Apr 17 '16 at 20:46






                  • 1





                    Also, your method will return a Product model, whilst using the Repository will give you a Product data model (Api/Data/Product), which is a Product model converted into a dumbed-down DTO. Something to consider, as they're quite different.

                    – nevvermind
                    Apr 18 '16 at 0:02











                  • @FabianSchmengler: Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working.

                    – Slimshadddyyy
                    Jan 31 '18 at 7:24











                  • @FabianSchmengler yes recommendation of Repository is good as @RiccardoT answer. but what if i entered wrong sku then it will break whole operation & throw exception, so in this case we must have to depend on productFactory

                    – Himanshu
                    Nov 5 '18 at 6:20











                  • @Himanshu catch the exception. And if you need a fresh product instance in that case, you can still create it using the factory

                    – Fabian Schmengler
                    Nov 5 '18 at 17:00








                  6




                  6





                  Actually while this is still working, using load() and collections is the "Magento 1" way, better use the repository as suggested by @RiccardoT.

                  – Fabian Schmengler
                  Apr 17 '16 at 20:46





                  Actually while this is still working, using load() and collections is the "Magento 1" way, better use the repository as suggested by @RiccardoT.

                  – Fabian Schmengler
                  Apr 17 '16 at 20:46




                  1




                  1





                  Also, your method will return a Product model, whilst using the Repository will give you a Product data model (Api/Data/Product), which is a Product model converted into a dumbed-down DTO. Something to consider, as they're quite different.

                  – nevvermind
                  Apr 18 '16 at 0:02





                  Also, your method will return a Product model, whilst using the Repository will give you a Product data model (Api/Data/Product), which is a Product model converted into a dumbed-down DTO. Something to consider, as they're quite different.

                  – nevvermind
                  Apr 18 '16 at 0:02













                  @FabianSchmengler: Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working.

                  – Slimshadddyyy
                  Jan 31 '18 at 7:24





                  @FabianSchmengler: Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working.

                  – Slimshadddyyy
                  Jan 31 '18 at 7:24













                  @FabianSchmengler yes recommendation of Repository is good as @RiccardoT answer. but what if i entered wrong sku then it will break whole operation & throw exception, so in this case we must have to depend on productFactory

                  – Himanshu
                  Nov 5 '18 at 6:20





                  @FabianSchmengler yes recommendation of Repository is good as @RiccardoT answer. but what if i entered wrong sku then it will break whole operation & throw exception, so in this case we must have to depend on productFactory

                  – Himanshu
                  Nov 5 '18 at 6:20













                  @Himanshu catch the exception. And if you need a fresh product instance in that case, you can still create it using the factory

                  – Fabian Schmengler
                  Nov 5 '18 at 17:00





                  @Himanshu catch the exception. And if you need a fresh product instance in that case, you can still create it using the factory

                  – Fabian Schmengler
                  Nov 5 '18 at 17:00











                  4














                  I like @phoenix128-riccardot 's answer, but would add an exception, just in case the product does not exist:




                  try {
                  $product = $this->productRepositoryInterface->get($sku);
                  } catch (MagentoFrameworkExceptionNoSuchEntityException $e){
                  // insert your error handling here
                  }


                  I was not able to add it as a comment (too low reputation), sorry.






                  share|improve this answer




























                    4














                    I like @phoenix128-riccardot 's answer, but would add an exception, just in case the product does not exist:




                    try {
                    $product = $this->productRepositoryInterface->get($sku);
                    } catch (MagentoFrameworkExceptionNoSuchEntityException $e){
                    // insert your error handling here
                    }


                    I was not able to add it as a comment (too low reputation), sorry.






                    share|improve this answer


























                      4












                      4








                      4







                      I like @phoenix128-riccardot 's answer, but would add an exception, just in case the product does not exist:




                      try {
                      $product = $this->productRepositoryInterface->get($sku);
                      } catch (MagentoFrameworkExceptionNoSuchEntityException $e){
                      // insert your error handling here
                      }


                      I was not able to add it as a comment (too low reputation), sorry.






                      share|improve this answer













                      I like @phoenix128-riccardot 's answer, but would add an exception, just in case the product does not exist:




                      try {
                      $product = $this->productRepositoryInterface->get($sku);
                      } catch (MagentoFrameworkExceptionNoSuchEntityException $e){
                      // insert your error handling here
                      }


                      I was not able to add it as a comment (too low reputation), sorry.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 27 '18 at 21:58









                      wherewhere

                      410212




                      410212























                          2














                          Try this:



                          /** @var MagentoCatalogModelProductFactory $productFactory */
                          $product = $productFactory->create();
                          $product->loadByAttribute('sku', 'my sku');

                          // $product->load($product->getId()); // may need to do this too,
                          // see MagentoFrameworkApiAttributeValueFactoryAbstractModel::loadByAttribute





                          share|improve this answer
























                          • I need to get the product_id from sku (column of csv) during csv import and save only the product_id..How to achieve?

                            – Sushivam
                            Aug 25 '16 at 9:45
















                          2














                          Try this:



                          /** @var MagentoCatalogModelProductFactory $productFactory */
                          $product = $productFactory->create();
                          $product->loadByAttribute('sku', 'my sku');

                          // $product->load($product->getId()); // may need to do this too,
                          // see MagentoFrameworkApiAttributeValueFactoryAbstractModel::loadByAttribute





                          share|improve this answer
























                          • I need to get the product_id from sku (column of csv) during csv import and save only the product_id..How to achieve?

                            – Sushivam
                            Aug 25 '16 at 9:45














                          2












                          2








                          2







                          Try this:



                          /** @var MagentoCatalogModelProductFactory $productFactory */
                          $product = $productFactory->create();
                          $product->loadByAttribute('sku', 'my sku');

                          // $product->load($product->getId()); // may need to do this too,
                          // see MagentoFrameworkApiAttributeValueFactoryAbstractModel::loadByAttribute





                          share|improve this answer













                          Try this:



                          /** @var MagentoCatalogModelProductFactory $productFactory */
                          $product = $productFactory->create();
                          $product->loadByAttribute('sku', 'my sku');

                          // $product->load($product->getId()); // may need to do this too,
                          // see MagentoFrameworkApiAttributeValueFactoryAbstractModel::loadByAttribute






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 17 '16 at 20:16









                          obscureobscure

                          2,0721431




                          2,0721431













                          • I need to get the product_id from sku (column of csv) during csv import and save only the product_id..How to achieve?

                            – Sushivam
                            Aug 25 '16 at 9:45



















                          • I need to get the product_id from sku (column of csv) during csv import and save only the product_id..How to achieve?

                            – Sushivam
                            Aug 25 '16 at 9:45

















                          I need to get the product_id from sku (column of csv) during csv import and save only the product_id..How to achieve?

                          – Sushivam
                          Aug 25 '16 at 9:45





                          I need to get the product_id from sku (column of csv) during csv import and save only the product_id..How to achieve?

                          – Sushivam
                          Aug 25 '16 at 9:45











                          2














                          You can try that



                          $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

                          $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');
                          $productObj = $productRepository->get('<SKU>');

                          echo $productObj->getId();





                          share|improve this answer




























                            2














                            You can try that



                            $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

                            $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');
                            $productObj = $productRepository->get('<SKU>');

                            echo $productObj->getId();





                            share|improve this answer


























                              2












                              2








                              2







                              You can try that



                              $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

                              $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');
                              $productObj = $productRepository->get('<SKU>');

                              echo $productObj->getId();





                              share|improve this answer













                              You can try that



                              $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 

                              $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');
                              $productObj = $productRepository->get('<SKU>');

                              echo $productObj->getId();






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 2 '18 at 12:25









                              Nadeem0035Nadeem0035

                              563410




                              563410























                                  0














                                  Using Dependency Injection (DI)



                                  Here is the example code to get the product information by product Id and SKU in Magento 2 using dependency injection.



                                  In this, we might need to inject the object of the MagentoCatalogModelProductRepository class in the constructor of our module’s block class and access it from the view ( .phtml ) file.



                                  Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php



                                  <?php
                                  namespace YourCompanyNameYourModuleNameBlock;

                                  class YourCustomBlock extends MagentoFrameworkViewElementTemplate
                                  {
                                  protected $_productRepository;

                                  public function __construct(
                                  MagentoFrameworkViewElementTemplateContext $context,
                                  MagentoCatalogModelProductRepository $productRepository,
                                  array $data = []
                                  ) {
                                  $this->_productRepository = $productRepository;
                                  parent::__construct($context, $data);
                                  }

                                  public function getProductById($id) {
                                  return $this->_productRepository->getById($id);
                                  }

                                  public function getProductBySku($sku) {
                                  return $this->_productRepository->get($sku);
                                  }
                                  }


                                  Now, we can use the functions in our view (.phtml) file as follows.



                                  // get product by id
                                  $product = $block->getProductById(15);

                                  // get product by sku
                                  $product = $block->getProductBySku('MT12');

                                  echo $product->getEntityId() . '<br>';
                                  echo $product->getName() . '<br>';
                                  echo $product->getSKU() . '<br>';
                                  echo $product->getPrice() . '<br>';
                                  echo $product->getSpecialPrice() . '<br>';
                                  echo $product->getTypeId() . '<br>';
                                  echo $product->getProductUrl() . '<br>';


                                  Using Object Manager



                                  Here is the example code to get the product information by product id and SKU in Magento 2 using object manager.



                                  $objectManager = MagentoFrameworkAppObjectManager::getInstance();

                                  $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');

                                  // get product by product id
                                  $product = $productRepository->getById(15);

                                  // get product by product sku
                                  $product = $productRepository->get('MT12');

                                  echo $product->getEntityId() . '<br>';
                                  echo $product->getName() . '<br>';
                                  echo $product->getSKU() . '<br>';
                                  echo $product->getPrice() . '<br>';
                                  echo $product->getSpecialPrice() . '<br>';
                                  echo $product->getTypeId() . '<br>';
                                  echo $product->getProductUrl() . '<br>';




                                  share




























                                    0














                                    Using Dependency Injection (DI)



                                    Here is the example code to get the product information by product Id and SKU in Magento 2 using dependency injection.



                                    In this, we might need to inject the object of the MagentoCatalogModelProductRepository class in the constructor of our module’s block class and access it from the view ( .phtml ) file.



                                    Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php



                                    <?php
                                    namespace YourCompanyNameYourModuleNameBlock;

                                    class YourCustomBlock extends MagentoFrameworkViewElementTemplate
                                    {
                                    protected $_productRepository;

                                    public function __construct(
                                    MagentoFrameworkViewElementTemplateContext $context,
                                    MagentoCatalogModelProductRepository $productRepository,
                                    array $data = []
                                    ) {
                                    $this->_productRepository = $productRepository;
                                    parent::__construct($context, $data);
                                    }

                                    public function getProductById($id) {
                                    return $this->_productRepository->getById($id);
                                    }

                                    public function getProductBySku($sku) {
                                    return $this->_productRepository->get($sku);
                                    }
                                    }


                                    Now, we can use the functions in our view (.phtml) file as follows.



                                    // get product by id
                                    $product = $block->getProductById(15);

                                    // get product by sku
                                    $product = $block->getProductBySku('MT12');

                                    echo $product->getEntityId() . '<br>';
                                    echo $product->getName() . '<br>';
                                    echo $product->getSKU() . '<br>';
                                    echo $product->getPrice() . '<br>';
                                    echo $product->getSpecialPrice() . '<br>';
                                    echo $product->getTypeId() . '<br>';
                                    echo $product->getProductUrl() . '<br>';


                                    Using Object Manager



                                    Here is the example code to get the product information by product id and SKU in Magento 2 using object manager.



                                    $objectManager = MagentoFrameworkAppObjectManager::getInstance();

                                    $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');

                                    // get product by product id
                                    $product = $productRepository->getById(15);

                                    // get product by product sku
                                    $product = $productRepository->get('MT12');

                                    echo $product->getEntityId() . '<br>';
                                    echo $product->getName() . '<br>';
                                    echo $product->getSKU() . '<br>';
                                    echo $product->getPrice() . '<br>';
                                    echo $product->getSpecialPrice() . '<br>';
                                    echo $product->getTypeId() . '<br>';
                                    echo $product->getProductUrl() . '<br>';




                                    share


























                                      0












                                      0








                                      0







                                      Using Dependency Injection (DI)



                                      Here is the example code to get the product information by product Id and SKU in Magento 2 using dependency injection.



                                      In this, we might need to inject the object of the MagentoCatalogModelProductRepository class in the constructor of our module’s block class and access it from the view ( .phtml ) file.



                                      Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php



                                      <?php
                                      namespace YourCompanyNameYourModuleNameBlock;

                                      class YourCustomBlock extends MagentoFrameworkViewElementTemplate
                                      {
                                      protected $_productRepository;

                                      public function __construct(
                                      MagentoFrameworkViewElementTemplateContext $context,
                                      MagentoCatalogModelProductRepository $productRepository,
                                      array $data = []
                                      ) {
                                      $this->_productRepository = $productRepository;
                                      parent::__construct($context, $data);
                                      }

                                      public function getProductById($id) {
                                      return $this->_productRepository->getById($id);
                                      }

                                      public function getProductBySku($sku) {
                                      return $this->_productRepository->get($sku);
                                      }
                                      }


                                      Now, we can use the functions in our view (.phtml) file as follows.



                                      // get product by id
                                      $product = $block->getProductById(15);

                                      // get product by sku
                                      $product = $block->getProductBySku('MT12');

                                      echo $product->getEntityId() . '<br>';
                                      echo $product->getName() . '<br>';
                                      echo $product->getSKU() . '<br>';
                                      echo $product->getPrice() . '<br>';
                                      echo $product->getSpecialPrice() . '<br>';
                                      echo $product->getTypeId() . '<br>';
                                      echo $product->getProductUrl() . '<br>';


                                      Using Object Manager



                                      Here is the example code to get the product information by product id and SKU in Magento 2 using object manager.



                                      $objectManager = MagentoFrameworkAppObjectManager::getInstance();

                                      $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');

                                      // get product by product id
                                      $product = $productRepository->getById(15);

                                      // get product by product sku
                                      $product = $productRepository->get('MT12');

                                      echo $product->getEntityId() . '<br>';
                                      echo $product->getName() . '<br>';
                                      echo $product->getSKU() . '<br>';
                                      echo $product->getPrice() . '<br>';
                                      echo $product->getSpecialPrice() . '<br>';
                                      echo $product->getTypeId() . '<br>';
                                      echo $product->getProductUrl() . '<br>';




                                      share













                                      Using Dependency Injection (DI)



                                      Here is the example code to get the product information by product Id and SKU in Magento 2 using dependency injection.



                                      In this, we might need to inject the object of the MagentoCatalogModelProductRepository class in the constructor of our module’s block class and access it from the view ( .phtml ) file.



                                      Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php



                                      <?php
                                      namespace YourCompanyNameYourModuleNameBlock;

                                      class YourCustomBlock extends MagentoFrameworkViewElementTemplate
                                      {
                                      protected $_productRepository;

                                      public function __construct(
                                      MagentoFrameworkViewElementTemplateContext $context,
                                      MagentoCatalogModelProductRepository $productRepository,
                                      array $data = []
                                      ) {
                                      $this->_productRepository = $productRepository;
                                      parent::__construct($context, $data);
                                      }

                                      public function getProductById($id) {
                                      return $this->_productRepository->getById($id);
                                      }

                                      public function getProductBySku($sku) {
                                      return $this->_productRepository->get($sku);
                                      }
                                      }


                                      Now, we can use the functions in our view (.phtml) file as follows.



                                      // get product by id
                                      $product = $block->getProductById(15);

                                      // get product by sku
                                      $product = $block->getProductBySku('MT12');

                                      echo $product->getEntityId() . '<br>';
                                      echo $product->getName() . '<br>';
                                      echo $product->getSKU() . '<br>';
                                      echo $product->getPrice() . '<br>';
                                      echo $product->getSpecialPrice() . '<br>';
                                      echo $product->getTypeId() . '<br>';
                                      echo $product->getProductUrl() . '<br>';


                                      Using Object Manager



                                      Here is the example code to get the product information by product id and SKU in Magento 2 using object manager.



                                      $objectManager = MagentoFrameworkAppObjectManager::getInstance();

                                      $productRepository = $objectManager->get('MagentoCatalogModelProductRepository');

                                      // get product by product id
                                      $product = $productRepository->getById(15);

                                      // get product by product sku
                                      $product = $productRepository->get('MT12');

                                      echo $product->getEntityId() . '<br>';
                                      echo $product->getName() . '<br>';
                                      echo $product->getSKU() . '<br>';
                                      echo $product->getPrice() . '<br>';
                                      echo $product->getSpecialPrice() . '<br>';
                                      echo $product->getTypeId() . '<br>';
                                      echo $product->getProductUrl() . '<br>';





                                      share











                                      share


                                      share










                                      answered 3 mins ago









                                      Amir HosseinzadehAmir Hosseinzadeh

                                      1013




                                      1013























                                          -4














                                          <?php
                                          use MagentoFrameworkAppBootstrap;
                                          require __DIR__ . '/app/bootstrap.php';
                                          $bootstrap = Bootstrap::create(BP, $_SERVER);
                                          $obj = $bootstrap->getObjectManager();
                                          $state = $obj->get('MagentoFrameworkAppState');
                                          $state->setAreaCode('frontend');


                                          $sku ='24-MB01';
                                          $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                                          $productObject = $objectManager->get('MagentoCatalogModelProduct');
                                          $product = $productObject->loadByAttribute('sku', $sku);
                                          echo $product->getName();

                                          ?>





                                          share|improve this answer




























                                            -4














                                            <?php
                                            use MagentoFrameworkAppBootstrap;
                                            require __DIR__ . '/app/bootstrap.php';
                                            $bootstrap = Bootstrap::create(BP, $_SERVER);
                                            $obj = $bootstrap->getObjectManager();
                                            $state = $obj->get('MagentoFrameworkAppState');
                                            $state->setAreaCode('frontend');


                                            $sku ='24-MB01';
                                            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                                            $productObject = $objectManager->get('MagentoCatalogModelProduct');
                                            $product = $productObject->loadByAttribute('sku', $sku);
                                            echo $product->getName();

                                            ?>





                                            share|improve this answer


























                                              -4












                                              -4








                                              -4







                                              <?php
                                              use MagentoFrameworkAppBootstrap;
                                              require __DIR__ . '/app/bootstrap.php';
                                              $bootstrap = Bootstrap::create(BP, $_SERVER);
                                              $obj = $bootstrap->getObjectManager();
                                              $state = $obj->get('MagentoFrameworkAppState');
                                              $state->setAreaCode('frontend');


                                              $sku ='24-MB01';
                                              $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                                              $productObject = $objectManager->get('MagentoCatalogModelProduct');
                                              $product = $productObject->loadByAttribute('sku', $sku);
                                              echo $product->getName();

                                              ?>





                                              share|improve this answer













                                              <?php
                                              use MagentoFrameworkAppBootstrap;
                                              require __DIR__ . '/app/bootstrap.php';
                                              $bootstrap = Bootstrap::create(BP, $_SERVER);
                                              $obj = $bootstrap->getObjectManager();
                                              $state = $obj->get('MagentoFrameworkAppState');
                                              $state->setAreaCode('frontend');


                                              $sku ='24-MB01';
                                              $objectManager = MagentoFrameworkAppObjectManager::getInstance();
                                              $productObject = $objectManager->get('MagentoCatalogModelProduct');
                                              $product = $productObject->loadByAttribute('sku', $sku);
                                              echo $product->getName();

                                              ?>






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jul 28 '17 at 5:44









                                              Nagaraju KasaNagaraju Kasa

                                              2,63721539




                                              2,63721539






























                                                  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%2f111245%2fhow-to-load-product-by-sku-in-magento-2%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)...

                                                  夢乃愛華...