10

In Magento 1.x I could do

if(Mage::getModel('catalog/product')->getIdBySku($sku))

How can I check this in Magento 2?

5 Answers 5

20
try {
    $product = $productRepository->get($sku);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e){
    $product = false;
}
1
  • This works exactly as I wanted.
    – TS Guhan
    Jun 7, 2019 at 12:38
12

Try this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product');

if($product->getIdBySku($sku)) {
    echo 'exit';    
}

OR

protected $product;
    public function __construct(
            \Magento\Catalog\Model\Product $product
    ) {
        $this->_product = $product;
    }

and after check

if($this->_product->getIdBySku($sku)) {
    echo 'exit';    
}
3
  • 2
    I thought you shouldn't use the objectManager directly
    – bpoiss
    Mar 18, 2016 at 11:41
  • updated ans pls check now
    – Abdul
    Mar 18, 2016 at 11:56
  • 2nd version without the object manager is better :) Jun 21, 2017 at 12:48
1

See @bpoiss answer, my answer is more about how to get a product using the sku.

To do so you can use the Magento\Catalog\Model\ProductRepository class.

First inject it in your constructor:

protected $_productRepository;

public function __construct(/* your original parameters here */, \Magento\Catalog\Model\ProductRepository $productRepository)
{
    /* your original code here */
    $this->_productRepository = $productRepository;
    /* end of your original code */
}

Then in your function you can do:

$this->_productRepository->get($sku);
4
  • 1
    This didn't work for me because if the sku exists i got an instance of ProductInterface and otherwise the string 'Product does not exist' which evaluetes to true. I would have to do an additional instance check
    – bpoiss
    Mar 18, 2016 at 11:40
  • @bpoiss yeah that's right my answer is more about how to get a product from the sku, your answer is definitely the right one ;) Btw, you can tick the 'answer my question' when you create a new question so you will have only one form to fill ;) Mar 18, 2016 at 11:50
  • thank you for pointing this out, but in the moment I wrote the question I did not know the answer ^^
    – bpoiss
    Mar 18, 2016 at 11:53
  • @bpoiss haha ok ^^ as your answer was quick I guessed you knew it. ;) Mar 18, 2016 at 11:53
0

Just found out that the getIdBySku method still exists in \Magento\Catalog\Model\Product. Inject it with

public function __construct(
    \Magento\Catalog\Model\Product $productModel
) {
    $this->productModel = $productModel;
}

and than you can check with

$this->productModel->getIdBySku($sku)
0

I think you can:

$checkProduct = $this->objectManager->create(
                'Magento\Catalog\Model\Product'
            );
    if($checkProduct->getIdBySku()){
    echo 'exist';
    }

But it is not the standard function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.