'Use Product Variants Select in Cart Prestashop

I'm developing a cross sell module that will display products in cart that you can add to cart by clicking the button add to cart. I need to take consideration of product variants (attributes).

I've already find out how to add the add to cart button to the cart page thanks to this code :

<form action="{$urls.pages.cart}" method="post" id="add-to-cart-or-refresh">
        <input type="hidden" name="token" value="{$static_token}">
        <input type="hidden" name="id_product" value="{$product->id}" id="product_page_product_id">
        <span class="remove-from-cart">
            <button class="btn btn-primary add-to-cart" data-button-action="add-to-cart" type="submit"
                {if (!$product->checkQty(1))} disabled {/if}>
                <i class="material-icons float-xs-left">shopping_cart</i>
                {l s="Add to cart" d='Shop.Theme.Actions'}
            </button>
        </span>
    </form>

But what I need now is to update the display price depending about the value chosen with the select option value dropdown set as on my product page and use the attribute id selected in order than when the user click on the add to cart button, it add to cart the product with the good attribute and not the default one

I'm sure that is from js script that the price is updated but I don't really find out which function is in charge of this

This is the select option as asked

      <div class="clearfix product-variants-item">
  <span class="control-label">ATTRIBUTE_GROUP_NAME</span>
          <select class="form-control form-control-select" id="group_ATTRIBUTE_GROUP_ID" data-product-attribute="ATTRIBUTE_GROUP_ID" name="group[ATTRIBUTE_GROUP_ID]">
                  <option value="ATTRIBUTE_ID" title="ATTRIBUTE_NAME" selected="selected">ATTRIBUTE_NAME</option>
              </select>
      </div>
  


Solution 1:[1]

the solution is not very simple but is achievable, the same way as prestashop works on product page, you have to ajax refresh your content on the attribute change. We developed a complex upselling module on checkout using the same method and it works good. The logical steps are:

-> Select value is changed

-> submit the data via ajax

-> get the fresh and updated data, best choice for me is to render your module view again and get an html as response

-> replace your content with the new html

Another way, not tested because little bit more complex in my opinion, could be to store in javascript all the data about all the variations/combinations and handle the logic there to get the price data you need on every change. A problem with this solution is that the data could be not always updated because stored locally.

I hope my hint helps, I wanted to comment but my ranking is still too low and I have to reply instead.

Simone

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simone Cabiddu