3

How can I edit this:

public function getUrlPath($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();
    }
    if ($this->isNeedToGenerateUrlPathForParent($category)) {
        $parentPath = $this->getUrlPath(
            $this->categoryRepository->get($category->getParentId(), $category->getStoreId())
        );
        $path = $parentPath === '' ? $path : $parentPath . '/' . $path;
    }
    return $path;
}

such that, the parent category is removed from the child categories' url.

For example:

www.mage.com/c1/c2/c3.html

becomes

www.mage.com/c3.html

1

We can configure it by URL Rewrites

  1. Go to Magento 2 Admin -> Marketing -> SEO & Search -> URL Rewrites

  2. Find the path by searching Request path column.

  3. Remove the cache php bin/magento cache:clean.

  4. Refresh the front-end, click the main menu category then you can see the configured path.

Example i have modified.

Request path men/tops-men/tees-men.htm

Modified path tees-men.htm

Or

You Can Build category URL path by

\vendor\magento\module-catalog-url-rewrite\Model\CategoryUrlPathGenerator.php 

Modify getUrlPath($category) by Magento Plug-in feature.

Hope this helps.

  • what do you mean by "Modify getUrlPath($category) by Magento Plug-in feature."? – rtk0407 Feb 1 '17 at 12:30

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.