Remove category_id from product URL

Developer
De United Kingdom
Membro desde out 2018

Hi Tim,

This add-on has been working well for me. It does however create duplicate links to the same product page.

For example a product can have multiple URLs if it exists in multiple categories:
https://www.abc.com/ducks
https://www.abc.com/ducks?category_id=20
https://www.abc.com/ducks?category_id=5

What I want is to remove the parameter "?category_id=#" overall so that a product will only have 1 URL (https://www.abc.com/ducks).

Please can you help?

Bishar

tim
Founder
De Sweden
Membro desde mai 2013
tim

So ?category_id is used to refer to a location in the category tree other than default category.
You will no longer have a pleasant result viewing a product found in more than one category if we remove this.

Developer
De United Kingdom
Membro desde out 2018

Hi Tim,

Well apart from the url containing the category_id and the breadcrumb showing the category name, the rest of the page is exactly the same. I don't really think I will miss much if we removed the category_id from the url.

Please can you guide me through it?

tim
Founder
De Sweden
Membro desde mai 2013
tim

In vmods/permalinks.xml. Find the directive for url_product.inc.php:



    <operation method="after" type="multiline" onerror="warning">
      <find><![CDATA[
      if (empty($link->query['product_id'])) return;
      ]]></find>

      <insert><![CDATA[
      $permalinks_query = database::query(
        "select * from ". DB_TABLE_PREFIX ."permalinks
        where entity = 'product'
        and entity_id = ". (int)$link->query['product_id'] ."
        and language_code = '". database::input($language_code) ."'
        limit 1;"
      );

      if ($permalink = database::fetch($permalinks_query)) {
        $link->unset_query('product_id');
        $link->path = $permalink['permalink'];
        return $link;
      }
      ]]></insert>
    </operation>
  </file>```

After
$link->unset_query('product_id');

Add
$link->unset_query('category_id');
Developer
De United Kingdom
Membro desde out 2018

Thank you Tim. That worked!

Tu
This website uses no cookies and no third party tracking technology. We think we can do better than others and really think about your privacy.