I have removed the category from the product URL by overriding getUrlPath
method in CategoryUrlPathGenerator
using a module and is working fine. But now I need to show the product page on both with and without category in URL. Is there a way I could achieve this? Below is the code in getUrlPath
.
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;
}