ale123 Posted October 15 Share Posted October 15 (edited) TL;DR: how can I generate a product link that automatically select an attribute option? --------------- I think there is a bug somewhere in product link generator. Let's assume we have a product with 3 options, like sizes: "S, M, L". Their IDs on DB are: 7,8,9 Now I would like to create a direct link to the product, with size "M" selected. I'm using this very long call: $this->context->link->getProductLink((new Product(87), null, null, null, null, null, 8); were 87 is the ID of the product and "8" is the ID of the attribute "M" Link is generated properly: /87-8-my-product.html But when opened, it does a redirect to the canonical url ignoring the attribute. Probably because the select in the attribute has a progressive ID (1,2,3) based on the option position in the select and not based on the attributes IDs Any idea ? Edited October 15 by ale123 (see edit history) Link to comment Share on other sites More sharing options...
ps8modules Posted October 15 Share Posted October 15 Hi. /** * Create a link to a product. * * @param ProductCore|array|int $product Product object (can be an ID product, but deprecated) * @param string|null $alias * @param string|null $category * @param string|null $ean13 * @param int|null $idLang * @param int|null $idShop (since 1.5.0) ID shop need to be used when we generate a product link for a product in a cart * @param int|null $idProductAttribute ID product attribute * @param bool $force_routes * @param bool $relativeProtocol * @param bool $withIdInAnchor * @param array $extraParams * @param bool $addAnchor * * @return string * * @throws PrestaShopException */ Link to comment Share on other sites More sharing options...
ale123 Posted October 16 Author Share Posted October 16 12 hours ago, ps8modules said: Hi. /** * Create a link to a product. * * @param ProductCore|array|int $product Product object (can be an ID product, but deprecated) * @param string|null $alias * @param string|null $category * @param string|null $ean13 * @param int|null $idLang * @param int|null $idShop (since 1.5.0) ID shop need to be used when we generate a product link for a product in a cart * @param int|null $idProductAttribute ID product attribute * @param bool $force_routes * @param bool $relativeProtocol * @param bool $withIdInAnchor * @param array $extraParams * @param bool $addAnchor * * @return string * * @throws PrestaShopException */ As wrote, that's the function i'm currently using and that is not working as expected. The link is created with the attribute inside, but when opened, there is a redirect to the canonical url and the attribute is not selected. Link to comment Share on other sites More sharing options...
ps8modules Posted October 16 Share Posted October 16 (edited) I have given you a list of parameters that need to be filled. $idProduct = 2; $idProductAttribute = 12; $product = new Product((int)$idProduct, false, $this->context->language->id); $link = new Link(); $linkProduct = $link->getProductLink( $product, false, false, false, $this->context->language->id, $this->context->shop->id, (int)$idProductAttribute, false, false, true, [], false ); Edited October 16 by ps8modules (see edit history) Link to comment Share on other sites More sharing options...
ale123 Posted October 16 Author Share Posted October 16 1 minute ago, ps8modules said: $idProduct = 2; $idProductAttribute = 12; $product = new Product((int)$idProduct, false, $this->context->language->id); $link = new Link(); $linkProduct = $link->getProductLink( $product, false, false, false, $this->context->language->id, $this->context->shop->id, (int)$idProductAttribute, false, false, true, [], false ); Again ? I'm already using that function call. It doesn't work, there is a redirect to the canonical url of the product. Link to comment Share on other sites More sharing options...
ps8modules Posted October 16 Share Posted October 16 There are big differences between your example and mine. Link to comment Share on other sites More sharing options...
ale123 Posted October 16 Author Share Posted October 16 The only difference is that you are not using the contex for the getProductLink Link to comment Share on other sites More sharing options...
ps8modules Posted October 16 Share Posted October 16 Use the example I gave you and you will see. I tested and everything is fine and working. Link to comment Share on other sites More sharing options...
ale123 Posted October 24 Author Share Posted October 24 I've found the issue: it's when multiple languages are enabled. Probably there is something wrong in my nginx rewrite rules because the direct product link with attributes, doesn't work as expected when multiple languages are enabled. If I disable the second lang, the product link works. Any tested and working configuration for nginx with multiple languages supported ? Link to comment Share on other sites More sharing options...
JBW Posted November 6 Share Posted November 6 Fifth parameter of the function is $idLang, did you try to fill it with $this->context->language->id as suggested? Link to comment Share on other sites More sharing options...
ale123 Posted November 6 Author Share Posted November 6 1 minute ago, JBW said: Fifth parameter of the function is $idLang, did you try to fill it with $this->context->language->id as suggested? Yes. The issue was some missing nginx rules Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now