1

I want to achieve product URLs based on product attribute for product page only. I have product attribute manufacturer, having drop downs such as lg, samsung, motorola etc.

My store category structure looks like as below:

Currently my product page opens directily as - https://www.mywebsite.com/product-1.html

I'm not using Use Categories Path for Product URLs of System/Configuration/Catalog/Search Engine Optimization. This is setted as false.

I wanted to rewrite or set product page URL in following way.

https://www.mywebsite.com/lg/product-1.html

https://www.mywebsite.com/samsung/product-2.html

Here, lg and samsung are having manufacturer options and it's value defined at product level. Values will be unique.

Also, I have other categories in which this product already belongs. So can not use categories path for product URLs as this creates same product duplicate issue with different category.

SEO settings correctly for this:

Use Categories Path for Product URLs -> NO
Create Permanent Redirect for URLs if URL Key Changed -> Yes
Use Canonical Link Meta Tag For Categories -> Yes
Use Canonical Link Meta Tag For Products -> No

Having single Magento 2.3 single store with single website, offering 1000 simple products.

1 Answer 1

1

I guess you have to use some custom code for this. For example you can write a short plugin like the following, which hooks in after the metod Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::getUrlKey and build you automatically an url key with brand and product name.

The plugin method could look like this:

public function afterGetUrlKey(\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator, $result, \Magento\Catalog\Model\Product $product)
{
    $brandPathInUrl = $product->formatUrlKey($product->getAttributeText('brand')); //change your attribute_code here
    if ($brandPathInUrl != "" ){
        $result = $brandPathInUrl . "/" . $product->formatUrlKey($product->getName());
    }
    return $result;
}

This method will generate and save an URL key composed of brand and name, separated by slash for each product you save. So for example you will see in the Admin panel for product 1 the URL key 'lg/product-1' and for product 2 'samsung/product-2'. But the process which is generating the rewrites will filter the slashes from you url key and will replace them with dashes. Therefore following this approach your urls will be:

https://www.mywebsite.com/lg-product-1.html
https://www.mywebsite.com/samsung-product-2.html

If that's an option for you, give it a try. Of course you can try to change the behaviour of formatUrlKey in the Product model to keep the slashes in your urls, but that wouldn't be a good idea in my opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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