0

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;
            }
2
  • 1
    Why don’t you wrap the $this->categoryRepository->get() in a try/catch block? Mar 29 at 13:50
  • @fantasticrice ty for idea Mar 31 at 8:19

0

Your Answer

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