There is one problem which I faced in my one of the Magento2 Project.
In Magento 2 Store, I’ve faced an issue with the product URL to access the product page. I need to remove the category path from the Product URL when I access any product page.
I found one admin configuration in Magento Admin at the below path to remove the category path from the product URL.
Path: Admin > STORES > Settings > Configuration > CATALOG > Catalog > Search Engine Optimization
- Set "Use Categories Path for Product URLs" option to No.
- After update this configuration, please clean config and full_page cache types and check the product URL.
Now, You can see the category path is removed when you access the product page from the product listing page.
But still, I can access the product page using the product URL with the category path.
For example,
Let’s take an example of the Magento Sample Data’s product name with "Olivia 1/4 Zip Light Jacket"
You can access the product detail page with below URLs by directly entering in the Address Bar of the browser:
- http://127.0.0.1/magento2/women/tops-women/jackets-women/olivia-1-4-zip-light-jacket.html
- It includes the category path in product URL
- http://127.0.0.1/olivia-1-4-zip-light-jacket.html
Here, I need to prevent the product page access when the URL includes the category path.
So As a solution, I decided If I got the category path in product URL I’ll redirect it to the product URL without category path.
I’ve achieved this solution by creating an after Plugin on findOneByData() function of the Magento\UrlRewrite\Model\Storage\AbstractStorage model class.
Please follow the below steps to implement this solution to omit the category path from the product URL to access the product page.
Find Complete module on github at Magelearn_Categoryurl
app/code/
Magelearn/Categoryurl<?php /** * Copyright © All rights reserved. * See COPYING.txt for license details. */ use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magelearn_Categoryurl', __DIR__);Add composer.json file in it:
{ "name": "magelearn/module-categoryurl", "description": "Magento2 remove/prevent category path from product url page", "type": "magento2-module", "license": "OSL-3.0", "authors": [ { "email": "info@mage2gen.com", "name": "Mage2Gen" }, { "email": "vijaymrami@gmail.com", "name": "vijay rami" } ], "minimum-stability": "dev", "require": {}, "autoload": { "files": [ "registration.php" ], "psr-4": { "Magelearn\\Categoryurl\\": "" } } }
<?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="Magelearn_Categoryurl" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog"/> <module name="Magento_UrlRewrite"/> </sequence> </module> </config>Add etc/di.xml file in it:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\UrlRewrite\Model\Storage\AbstractStorage"> <plugin disabled="false" name="Plugin_Magento_Checkout_Block_Onepage" sortOrder="10" type="Magelearn\Categoryurl\Plugin\FindOneByData"/> </type> </config>
<?php namespace Magelearn\Categoryurl\Plugin;
use Magento\Framework\App\Config\ScopeConfigInterface; /** * Class FindOneByData * @package Magelearn\Categoryurl\Plugin
*/ class FindOneByData { /** * @var ScopeConfigInterface */ protected $scopeConfig; /** * FindOneByData constructor. * @param ScopeConfigInterface $scopeConfig */ public function __construct( ScopeConfigInterface $scopeConfig ) { $this->scopeConfig = $scopeConfig; } /** * @param \Magento\UrlRewrite\Model\Storage\AbstractStorage $subject * @param $result * @param array $data * @return mixed */ public function afterFindOneByData( \Magento\UrlRewrite\Model\Storage\AbstractStorage $subject, $result, array $data ) { if(!empty($result) && !(bool)$this->scopeConfig->getValue('catalog/seo/product_use_categories') && $result->getEntityType() == 'product' && (strpos(trim($result->getRequestPath(), '/'), '/') !== false) ) { $requestPathArr = explode('/', $result->getRequestPath()); if (count($requestPathArr) > 1) { $newRequestPath = end($requestPathArr); $result->setTargetPath($newRequestPath); $result->setRedirectType(301); } } return $result; } }
0 Comments On "How to prevent/omit category path from product URL to access the product page?"