Jump to content

Direct link to a product with a selected attribute


Recommended Posts

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 by ale123 (see edit history)
Link to comment
Share on other sites

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

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

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 by ps8modules (see edit history)
Link to comment
Share on other sites

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

  • 2 weeks later...

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

  • 2 weeks later...
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...