How to load product by SKU in magento 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 = \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 API’s, but nothing seems to work with SKU’s.

Solutions

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

public function __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));

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; 
...
public function __construct(
    ...
    \Magento\Catalog\Api\ProductRepositoryInterface $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.

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 (\Magento\Framework\Exception\NoSuchEntityException $e){
    // insert your error handling here
}

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

Similar questions

how to change SKU validation message in order by sku tab in magento 2.1
I want to change SKU error message: Please enter valid SKU key To Please enter a valid SKU key in magento 2.1 , can anyone help me please. Thank you
Magento 1.7: Different sku and custom option sku for ERP System import
The customer should get the sku of the product in the catalog, cart and checkout process to avoid missunderstanding as this standard sku is communicated in the price list, the custom options has no price difference as it handles technical features like country specific cables or different frequencies. For the import to the erp system we need the cu...
Magento 2 REST - SKU to items on cart totals API response is not available, How to get SKU in response API
Magento 2 Rest API of cart totals doesn't provide SKU, Which is necessary for any operation like extension_attributes The return array of Items is I want to add extension attribute of Image but I need SKU for that, But in this return data Magento doesn't provide SKU. File is used for API Function is used for API Any one have Fixed the above issue, ...
How to get parent sku from child sku magento 2
I have a child SKU in an array and I need to get all the parents from the existing child SKUs from the array. Can everybody help me ? Thanks
Magento: Remove Dash from between sku and custom option sku
I am looking to remove the "-" in our SKU's when a customer purchases a product that has a custom option. For example they buy a shirt and choose custom option size 28 The SKU in the order becomes shirt-28 We need it to become instead shirt28 I think I am supposed to edit but cannot identify the line. Thanks in advanced. James
Get parent SKU (configurable or bundle) from child SKU Magento 2
On the order page(backend) I need to be able to obtain the parent SKU using a child SKU. I've tried several code snipped from both the Magento forums and similar questions here on StackOverflow without success. I'm able to determine if a product is just a simple product without a parent by using getTypeId() but after that everything I try fails to ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.