After update Magento 2.3.5 to 2.4.2 on all category pages error: Exception #0 (RuntimeException): Catalog Layer has been already created. How fix?

magento.stackexchange https://magento.stackexchange.com/questions/331301

Question

After updating the version, I started getting on all category pages this error: Exception #0 (RuntimeException): Catalog Layer has been already created

enter image description here

This is error in: vendor/magento/module-catalog/Model/Layer/Resolver.php

 /**
 * Create Catalog Layer by specified type
 *
 * @param string $layerType
 * @return void
 */
public function create($layerType)
{
    if (isset($this->layer)) {
        throw new \RuntimeException('Catalog Layer has been already created');
    }
    if (!isset($this->layersPool[$layerType])) {
        throw new \InvalidArgumentException($layerType . ' does not belong to any registered layer');
    }
    $this->layer = $this->objectManager->create($this->layersPool[$layerType]);
}

$layerType = 'category' from vendor/magento/module-catalog/Controller/Category/View.php

 /**
 * Category view action
 *
 * @throws NoSuchEntityException
 */
public function execute()
{
    $result = null;

    if ($this->_request->getParam(ActionInterface::PARAM_NAME_URL_ENCODED)) {
        return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
    }
    $category = $this->_initCategory();
    if ($category) {
        $this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
        $settings = $this->_catalogDesign->getDesignSettings($category);

        // apply custom design
        if ($settings->getCustomDesign()) {
            $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
        }

        $this->_catalogSession->setLastViewedCategoryId($category->getId());

        $page = $this->resultPageFactory->create();
        // apply custom layout (page) template once the blocks are generated
        if ($settings->getPageLayout()) {
            $page->getConfig()->setPageLayout($settings->getPageLayout());
        }

        $pageType = $this->getPageType($category);

        if (!$category->hasChildren()) {
            // Two levels removed from parent.  Need to add default page type.
            $parentPageType = strtok($pageType, '_');
            $page->addPageLayoutHandles(['type' => $parentPageType], null, false);
        }
        $page->addPageLayoutHandles(['type' => $pageType], null, false);
        $page->addPageLayoutHandles(['displaymode' => strtolower($category->getDisplayMode())], null, false);
        $page->addPageLayoutHandles(['id' => $category->getId()]);

        // apply custom layout update once layout is loaded
        $this->applyLayoutUpdates($page, $settings);

        $page->getConfig()->addBodyClass('page-products')
            ->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
            ->addBodyClass('category-' . $category->getUrlKey());

        return $page;
    } elseif (!$this->getResponse()->isRedirect()) {
        $result = $this->resultForwardFactory->create()->forward('noroute');
    }
    return $result;
}

But i can't found where loaded Category Layer too. I check this issue in Google and i found this issue, but without answer. I have Amasty modules like Amasty_Shopby and required modules for him and after disabling this modules i am not getting this error. How can I fix this error without turning off these modules?

Please, help with this issue! Thanks!

Was it helpful?

Solution

It seems that this is a compatibility issue. I briefly checked the Magento marketplace and found the Shop By Brand extension (I am not sure if you are using this particular one, I am just assuming you do), and in the compatibility section it mentions: 2.3, 2.2, 2.1 and NOT 2.4.

My intuition tells me that whichever extension you use in the 'shop by' suite, there is a high possibility to have compatibility issues. I've seen those extensions at some point and they all rely on a core module.

Unfortunately, unless someone takes a debugger and goes through the code to see which is the problem and also understand the project architecture and other customizations, it is impossible to just provide a solution here.

This is something that we are facing constantly when installing 3rd party modules in the context of Magento upgrades. There are many case where the vendors don't keep up with the Magento quarterly upgrades (with new standards and/ or backwards incompatible changes introduction) and we end up having delayed Magento upgrades.

Sorry mate, I think right now, your options are:

  1. Contact Amasty and ask for support. If you payed for the extensions then you might get lucky. Although I am not sure they will solve a compatibility issue anytime soon.
  2. Turn off the modules and favor custom code developed on the 2.4.2 edition to cover and accommodate your business needs.
  3. Don't upgrade Magento and keep your Amasty stuff running (NOT RECOMMENDED).
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top