Hi I have this problem when I delete a category from admin area. If a user clicks on a url of a deleted product/category, or if he manually enters the id of the non-existent category, he generates a no such entity id exception. I Check in database in all table and colunm and no found deleted category id. If i check var/report see this
{"0":"No such entity with id = 40","1":"#1 Magento\Catalog\Model\CategoryRepository->get() called at [app/code/Manadev/LayeredNavigation/FilterTemplates/Category/TextSingleSelect.php:81]
Is third part extension and the code is this
public function prepare(ProductCollection $productCollection, Filter $filter) {
$name = $filter->getData('param_name');
$query = $productCollection->getQuery();
$appliedCategory = false;
if (($appliedCategoryId = $this->requestParser->readSingleValueInteger($name)) !== false) {
$query->getFilterGroup('layered_nav')->addOperand($this->factory->createLayeredCategoryFilter(
$name, [$appliedCategoryId]));
/* @var $appliedCategory Category */
$appliedCategory = $this->categoryRepository->get($appliedCategoryId,$this->storeManager->getStore()->getId());
}
$query->addFacet($this->factory->createChildCategoryFacet($name, $appliedCategory,
$filter->getData('hide_filter_with_single_visible_item')));
}
This string $appliedCategory = $this->categoryRepository->get($appliedCategoryId,$this->storeManager->getStore()->getId()); give me a problem because if no found getId() give me ecception maybe with control can fix this?
I think that's how I fixed it
use Magento\Framework\Controller\Result\RedirectFactory; RedirectFactory $resultRedirectFactory in the constructor
try {
$appliedCategory = $this->categoryRepository->get($appliedCategoryId, $this->storeManager->getStore()->getId());
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
// Set appliedCategory to null
$appliedCategory = null;
// Redirect to custom 404 page
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('no-route');
return $resultRedirect;
}
$this->categoryRepository->get()
in atry/catch
block?