Prestashop 1.7中getProductLink TPL链接重定向问题求助
Hey there! I get it, dealing with unexpected URL redirects after a PrestaShop upgrade can be super frustrating—especially when your custom module stops generating the links you expect. Let's break down what's happening and how to fix it.
What's Causing the Redirect?
From what you described, your generated URL (art/3-affiche-encadree-the-best-is-yet-to-come.html) is being redirected to one with an extra number (art/3-13-...). That extra number is almost certainly the id_product_attribute (the ID for a product's attribute combination, like size or color).
PrestaShop 1.7 changed how it handles URLs for products with attributes. Your SEO setting {Category: / {id}} {-: id_product_att...} tells PrestaShop to include the attribute ID in the URL when a product has attributes (the {-: ...} part means "only show this if the value exists"). When you generate a link without the attribute ID, PrestaShop automatically redirects to the URL that matches your SEO rules—adding the default attribute's ID in the process.
Solutions to Try
1. Force getProductLink to Skip Attribute IDs
When you call getProductLink, you can explicitly pass 0 as the id_product_attribute parameter to tell PrestaShop not to include any attribute ID in the URL.
Here's how to adjust your code in the Tpl/PHP file:
// Original call (might not specify the attribute ID) $productLink = $this->context->link->getProductLink($product); // Updated call with id_product_attribute set to 0 $productLink = $this->context->link->getProductLink( $product, null, // $alias null, // $category null, // $ean13 null, // $id_lang null, // $id_shop 0 // $id_product_attribute (force no attribute ID) );
2. Adjust Your SEO URL Rule
If you don't want attribute IDs in your product URLs at all, you can modify the SEO setting in your PrestaShop backend:
- Go to Shop Parameters > Traffic & SEO > SEO & URLs
- Find the "Product URL" pattern and remove the part related to
id_product_attribute - For example, change
{Category: / {id}} {-: id_product_attribute-}to just{Category: / {id}}
After making this change, clear your PrestaShop cache (Advanced Parameters > Performance) to ensure the new rule takes effect.
3. Check the Product's Default Attribute
If the product has a default attribute combination set, PrestaShop 1.7 will automatically include its ID in the URL. You can:
- Edit the product in the backend
- Go to the Combinations tab
- Remove the default attribute (set "Default combination" to "None")
This will stop PrestaShop from adding the attribute ID to the URL for that product.
Final Notes
After making any changes, be sure to test the generated links to confirm they no longer redirect. Also, clear both the PrestaShop cache and your browser cache to avoid old URL rules interfering.
内容的提问来源于stack exchange,提问作者Akim Prestatic

