I am adding a product to the cart using MagentoCatalogModelProduct and works fine if the product id exist.
public function __construct(
...
MagentoCatalogModelProduct $product,
...
)
{
...
$this->_product = $product;
...
}
try{
$product = $this->_product->load($productID);
}catch (MagentoFrameworkExceptionLocalizedException $e) {
$response = $this->resultJsonFactory->create();
$response->setData(['message' => 'Subscription id not founded']);
$response->setHttpResponseCode(406);
return $response;
}
but I get an exemption when the id don’t exist:
1 exception(s): Exception #0 (MagentoFrameworkExceptionLocalizedException): The product wasn't found. Verify the product and try again. Exception #0 (MagentoFrameworkExceptionLocalizedException): The product wasn't found. Verify the product and try again.
#1 MagentoCheckoutModelCart->addProduct(&MagentoCatalogModelProductInterceptor#000000001b3988ae0000000074f86046#, array('product' => 1, 'qty' => 1)) called at [vendor/magento/framework/Interception/Interceptor.php:58] #2 MagentoCheckoutModelCartInterceptor->___callParent('addProduct', array(&MagentoCatalogModelProductInterceptor#000000001b3988ae0000000074f86046#, array('product' => 1, 'qty' => 1))) called at [vendor/magento/framework/Interception/Interceptor.php:138]How can I avoid the code to brake. I tried try and catch or
if(this->_product->load($productID);))
But nothing working.Thanks