2

I am unable to save Category from magento admin.

When i click on save button below error displaying:

Notice: Undefined index: category_id in vendor/magento/module-catalog-url-rewrite/Model/Category/Plugin/Storage.php on line 49

Please help me to fix this issue.

5
  • 1
    Try composer update and see error still persist or not. Commented Sep 21, 2016 at 10:53
  • Any updates on this? Did you fix it?
    – Roy
    Commented Oct 20, 2016 at 14:36
  • Unassigned all the products and saved the category, then again assigned the required products to the category. Now working fine Commented Oct 26, 2016 at 8:04
  • @Sudheer: Are you able to fix this issue, please let me know, I also facing this issue. Commented Jun 6, 2018 at 6:09
  • @PurushotamSharma: yes Commented Jul 2, 2018 at 15:23

4 Answers 4

8

Please update your vendor file Storage.php

vendor/magento/module-catalog-url-rewrite/Model/Category/Plugin/Storage.php

$toSave[] = [
                'url_rewrite_id' => $record->getUrlRewriteId(),
                'category_id' => $metadata['category_id'],
                'product_id' => $record->getEntityId(),
            ];

with the below code

if(!empty($metadata)){
                $toSave[] = [
                    'url_rewrite_id' => $record->getUrlRewriteId(),
                    'category_id' => $metadata['category_id'],
                    'product_id' => $record->getEntityId(),
                ];
            }
5
  • Better override this file instead of directly editing the core file.
    – MGento
    Commented Aug 20, 2018 at 6:27
  • this is not working.... Commented Nov 19, 2018 at 12:00
  • This is working fine !!!! Commented Sep 15, 2019 at 7:02
  • that works thank you. I have magento 2.3.4 if somebody wants to know
    – Ilijanovic
    Commented Apr 29, 2020 at 5:24
  • Work for me magento2.3.3
    – HaFiz Umer
    Commented Jul 28, 2020 at 11:31
2

Just in case anyone comes across this. I encountered the same issue and used the same solution as above but used the override method as editing vendor files is not recommended.

Vendor/Module/etc/adminhtml/di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage" type="Vendor\Module\Model\Category\Plugin\Storage" />
</config>

Vendor/Module/Model/Category/Plugin/Storage.php

namespace Vendor\Module\Model\Category\Plugin;

use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product;
use Magento\UrlRewrite\Model\StorageInterface;
use Magento\UrlRewrite\Model\UrlFinderInterface;

class Storage extends \Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage
{
    /**
     * @var Product
     */
    protected $productResource;

    /**
     * Storage constructor.
     * @param UrlFinderInterface $urlFinder
     * @param Product $productResource
     */
    public function __construct(UrlFinderInterface $urlFinder, Product $productResource)
    {
        parent::__construct($urlFinder, $this->productResource = $productResource);
    }

    /**
     * @param StorageInterface $object
     * @param array $result
     * @param array $urls
     * @return array|\Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
     */
    public function afterReplace(StorageInterface $object, array $result, array $urls)
    {
        $toSave = [];
        foreach ($this->filterUrls($result) as $record) {
            $metadata = $record->getMetadata();
            if (!empty($metadata)) {
                $toSave[] = [
                    'url_rewrite_id' => $record->getUrlRewriteId(),
                    'category_id' => $metadata['category_id'],
                    'product_id' => $record->getEntityId(),
                ];
            }
        }
        if (count($toSave) > 0) {
            $this->productResource->saveMultiple($toSave);

        }
        return $result;
    }
}
1

I also had this problem when was trying to save category with a huge amount of products. Try to switch "Store View" to some store (not "All store views") on the top of the category admin page and it should help

1
  • This actually worked. M2 store with many M1 migrated products and occasional weird behavior. Commented Jan 19, 2021 at 16:08
0

I had the same issue with 2.2.5 I could add the products to the category AFTER i have added the products to the correct website (update attributes => assign to website)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.