0% found this document useful (0 votes)
2K views2 pages

Magento2 - How To Load Product by SKU in Magento 2

There are three main ways to load a product by SKU in Magento 2: 1. Inject and use the ProductRepositoryInterface which returns a Product data model object. 2. Inject and use the ProductFactory to load by attribute or get ID and load. This returns a Product model object. 3. Load directly using the product model but this is not recommended. The recommended approach is to inject and use the ProductRepositoryInterface which follows the service contract approach of Magento 2.

Uploaded by

m1k13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views2 pages

Magento2 - How To Load Product by SKU in Magento 2

There are three main ways to load a product by SKU in Magento 2: 1. Inject and use the ProductRepositoryInterface which returns a Product data model object. 2. Inject and use the ProductFactory to load by attribute or get ID and load. This returns a Product model object. 3. Load directly using the product model but this is not recommended. The recommended approach is to inject and use the ProductRepositoryInterface which follows the service contract approach of Magento 2.

Uploaded by

m1k13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

signup login tour help

_
Magento Stack Exchange is a question Here's how it works:
and answer site for users of the
Magento e-Commerce platform. Join
them; it only takes a minute:

Sign up Anybody can ask Anybody can The best answers are voted
a question answer up and rise to the top

How to load product by SKU in magento 2

It's pretty basic, but I cant nd 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=\Magento\Framework\App\ObjectManager::getInstance()

$product=$objectManager>create('\Magento\Catalog\Api\Data\ProductInterface')

$product>get('<SKU>')

$product>getName()

This returns nothing. I also tried loading any available models and APIs, but nothing seems to work with SKUs.

magento2 product

asked Apr 17 '16 at 20:07


Dennis van Schaik
136 1 11

3 Answers

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

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

Use Magento\Catalog\Api\ProductRepositoryInterface to get it in your constructor.

Full example:

...
private$productRepository
...
publicfunction__construct(
...
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository
...
){
...
$this>productRepository=$productRepository
...
}

publicfunctionloadMyProduct($sku)
{
return$this>productRepository>get($sku)
}
...

edited Dec 16 '16 at 17:25 answered Apr 17 '16 at 20:42


RiccardoT
2,950 1 1 14

3 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

can you pls add more details about UseMagento\Catalog\Api\ProductRepositoryInterfacetoget


itinyourconstructor.? What shall I do? Thanks a lot davideghz Dec 16 '16 at 17:17

See my updated post RiccardoT Dec 16 '16 at 17:25

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

publicfunction__construct(\Magento\Catalog\Model\ProductFactory$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))

answered Apr 17 '16 at 20:41


Fabian Schmengler
36k 13 76 210

1 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

@nevvermind this leads to an interesting follow-up question: magento.stackexchange.com/questions/111286/


Fabian Schmengler Apr 18 '16 at 7:24

Try this:

/**@var\Magento\Catalog\Model\ProductFactory$productFactory*/
$product=$productFactory>create()
$product>loadByAttribute('sku','mysku')

//$product>load($product>getId())//mayneedtodothistoo,
//see\Magento\Framework\Api\AttributeValueFactory\AbstractModel::loadByAttribute

answered Apr 17 '16 at 20:16


obscure
702 5 18

I need to get the product_id from sku (column of csv) during csv import and save only the product_id..How to achieve?
Sachin S Aug 25 '16 at 9:45

You might also like