Remove parent category name form the url of subcategory

Remove parent category name form the url of subcategory
  • There was specific control in configuration in admin in magento 1 to remove parent category name from the child category URL, while in magento 2 there is no such configuration so for that you have to overwrite the vendor code.

Please follow below steps :

1.) first add dependency into Tridhyatech/RemoveParentUrl/etc/di.xml

2.) add below code into Tridhyatech\ RemoveParentUrl \Model\CategoryUrlPathGenerator

namespace Tridhyatech\Products\Model;

use Magento\Catalog\Model\Category;

class CategoryUrlPathGenerator extends \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator

{

public function getUrlPath($category, $parentCategory = null)

{

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;

}

}



Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.