Magento 2

Allowed memory size exhausted on category save Magento 2

The issue is related to the new way of URL rewrites generator in Magento2. In Magento2, URL rewrites are generated (in url_rewrite table) when user save the category. If you have a complex category structure and assign a category as child of multiple parents or using categories across different store_views , it tries to generate the URL for all combinations and hence get out of memory.

Below is not the solution, it’s workaround:

Open the file /vendor/magento/module-catalog-url-rewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php and comment the code in the execute function.

<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogUrlRewrite\Observer;

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\Framework\Event\ObserverInterface;

class CategoryProcessUrlRewriteSavingObserver implements ObserverInterface
{
    /** @var CategoryUrlRewriteGenerator */
    protected $categoryUrlRewriteGenerator;

    /** @var UrlPersistInterface */
    protected $urlPersist;

    /** @var UrlRewriteHandler */
    protected $urlRewriteHandler;

    /**
    * @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
    * @param UrlPersistInterface $urlPersist
    * @param UrlRewriteHandler $urlRewriteHandler
    */
    public function __construct(
        CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator,
        UrlPersistInterface $urlPersist,
        UrlRewriteHandler $urlRewriteHandler
    ) {
        $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator;
        $this->urlPersist = $urlPersist;
        $this->urlRewriteHandler = $urlPersist;
    }

    /**
    * Generate urls for UrlRewrite and save it in storage
    *
    * @param \Magento\Framework\Event\Observer $observer
    * @return void
    */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var Category $category */
        /* $category = $observer->getEvent()->getCategory();
        if ($category->getParentId() == Category::TREE_ROOT_ID) {
            return;
        }
        if ($category->dataHasChangedFor(‘url_key’)
            || $category->dataHasChangedFor(‘is_anchor’)
            || $category->getIsChangedProductList()
        ) {
            $urlRewrites = array_merge(
            $this->categoryUrlRewriteGenerator->generate($category),
            $this->urlRewriteHandler->generateProductUrlRewrites($category)
            );
            $this->urlPersist->replace($urlRewrites);
        } */
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *