1

I'm attempting to scan an incoming list of products and add any new ones to the catalog. Therefore, I must run a check on each product to see if it already exists.

I'm using a ProductRepositoryInterface via DI to load each product by SKU, but if the product doesn't exist within the catalog (new product), it fails with a \Magento\Framework\Exception\NoSuchEntityException.

How can I write logic around this so I can either: confirm the product exists, skip it gracefully, and move on OR confirm the product doesn't exist, and then add it? Is there something like an exists() method that I'm missing?

$product = $this->productRepository->get($xmlProductSku);
0

Same issue here Magento2 - ProductRepository always throws NoSuchEntityException

You'd use the concrete Exception class Magento are throwing in the try/catch block

-2

What's the problem to catch exception...

try {
    $product = $this->productRepository->get($xmlProductSku);
} catch (\Exception $e) {
    $product = false;
}

if ($product) {
   //some logic
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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