I've deleted product from backend.

Now I'm loading product in batch by using productRepository in helper.

In that process I've that deleted product Id so when ever process comes to following line

return $this->_productRepository->getById($id);

I'm getting that error. So now what can be done to check if product is deleted then just return null.

   return $this->_productRepository->getById($id);
   return ;

Above code also return the same error and execution get stopped.

  • are you want to pass null if product not found? – Rakesh Jesadiya Mar 28 '17 at 7:23
  • yeah but even checking in if it throws error. – Kaushal Suthar Mar 28 '17 at 9:03
  • please let me know if you have query – Rakesh Jesadiya Mar 28 '17 at 9:46

You have to set inside try/catch block and check,

try {
    $collection = $this->_productRepository->getById($id);;
} catch (\Exception $exception) {
    $collection = null;
return $collection;
  • How can I check if product is deleted or not because that error is coming because product was deleted... – Kaushal Suthar Mar 28 '17 at 9:48
  • At those place when you have faced this error. you can keep it above logic and check again same error are display? i have same error occure in past. – Rakesh Jesadiya Mar 28 '17 at 9:49
  • you can close this question after accept as answer if you helpful, so other community users have useful in future. – Rakesh Jesadiya Mar 28 '17 at 10:40
  • It will still throw an error when it reach to $collection = $this->_productRepository->getById($id); – Kaushal Suthar Mar 28 '17 at 11:11

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.