1

Referring the answer to this question - How to remove parent category from subcategory URL? I created the file in Module Plugin folder - RemoveParentCategoryPathPlugin.php with contents like:

<?php
namespace Myweb\RemoveParentCategoryPathPlugin\Plugin;

class RemoveParentCategoryPathPlugin
{
    public function aroundGetUrlPath($subject, $proceed, $category)
    {
        if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
            return '';
        }
        $path = $category->getUrlPath();
        if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) {
            return $path;
        }
        $path = $category->getUrlKey();
        if ($path === false) {
            return $category->getUrlPath();
        }
        return $path;
    }
}

also added di.xml in etc folder with contents like:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogUrlRewrite\Model">
        <plugin name="Myweb_RemoveParentCategoryPathPlugin" type="Myweb\RemoveParentCategoryPathPlugin\Plugin\RemoveParentCategoryPathPlugin" />
    </type>
</config>

and module.xml in etc folder

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Myweb_RemoveParentCategoryPathPlugin" setup_version="1.0.1" />
</config>

and registration.php in module folder with contents:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Myweb_RemoveParentCategoryPathPlugin',
__DIR__
);

What I am missing here. as the plugin doesn't seems to have any effect. I tried to create new category as well to see if it works on new category.

1

You need to modify your di.xml like below:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator">
        <plugin name="Myweb_RemoveParentCategoryPathPlugin" type="Myweb\RemoveParentCategoryPathPlugin\Plugin\RemoveParentCategoryPathPlugin" />
    </type>
</config>

You have missed the class name in your di.xml.

Run the following command after the change:

php bin/magento setup:upgrade
php bin/magento setup:di:compile
  • Hi Sukumar - Thanks for your help. When I am running the command setup:di:compile I am getting the error from other module ccavenue_pay like ` [ReflectionException] Class Magento\Ccavenuepay\Helper\Hss does not exist` I have disabled that module temporarily, but I am still getting this error. how I can overcome that. – Joshi Jul 7 '18 at 16:31
  • Copy that module from your magento2 installation to outside and delete the ccavenue from magento2 then run the command. – Sukumar Gorai Jul 7 '18 at 16:32
  • Thanks I did just that. and was able to run the command "setup:di:compile". Now when I am saving an existing category I am getting the error like - something went wrong while saving the category and if I am creating a new category some more info with the same error like Warning: ltrim() expects parameter 1 to be string, array given in /home/wwwindianartzone/public_html/vendor/magento/module-catalog/Model/Category/FileInfo.php on line 102 . Any idea why I am getting that error. – Joshi Jul 7 '18 at 17:41
  • This issue is something different. – Sukumar Gorai Jul 7 '18 at 17:42
  • You mean it is not related to the code example I have added in the question. – Joshi Jul 7 '18 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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