skip to Main Content

id_product_attribute is available in URL – value “10”:
http://localhost/presta/women/2-10-brown-(…).html#/2-size-m

I need to get current id_product_attribute from current product page. May be from $_GET, or from DOM element, or presta shop variable – but I have to have it passed to JavaScript function, before add to cart (even if finally, customer don’t add product to the cart – that’s why I can’t use hook: “actionCartSave”)

I have access to this value from hook displayAfterProductThumbs – but is problem with getting current value. To get correct value, I need to:

1) choose product attributes on product page (size, color)

2) refresh page to trigger hook “displayAfterProductThumbs”

3) read data

But I need it without refreshing.

In documentation I can’t found nothing about that. Tried to find for phrases: id_product_attribute , id_combination , idCombination , ipa. Most information about id_product_attribute (found on Google) is related to SEO and “not good idea to have id_product_attribute in url for SEO purposes”.

3

Answers


  1. Hi @DamianSobkowiak and welcome to SO 🙂

    In PrestaShop 1.6.x and older versions, you could retrieve this ID by using the idProductAttribute global JS variable.

    In PrestaShop 1.7.x versions, product attributes (size, color, etc.) IDs selected by the buyer are stored in the group variable within an array, however this variable no longer contains the related id_product_attribute itself.

    When a product is added to cart, the /controllers/front/CartController.php file is called and you can see the following on line 366:

    $this->id_product_attribute = (int)Product::getIdProductAttributeByIdAttributes($this->id_product, Tools::getValue('group'));
    

    A solution for you could be to:

    1. Add a .js file to your theme with an event listener on the “Add to cart” button (use event.preventDefault() to make sure you’ll have time to process the next steps)
    2. In case this event is triggered, make an Ajax call to a controller file that you will create with the following code:

      if (isset($_GET['group']) && is_array($_GET['group']) && isset($_GET['id_product']))
      {
      include('config/config.inc.php');
      echo (int)Product::getIdProductAttributeByIdAttributes((int)$_GET['id_product'], $_GET['group']);
      }

      Don’t forget to pass the group and id_product values when making your ajax call.

    3. Retrieve the result of the ajax call and store the id_product_attribute into a variable

    I hope this helps!

    Login or Signup to reply.
  2. There is a hook called displayProductAdditionalInfo.
    Register the hook in the module and in the parameter you get the product detail.
    This code will run when we change the combination of the product.
    So the idProductAttribute will get updated automatically on change of combination.

    public function hookDisplayProductAdditionalInfo($params)
    {
        if isset($params['product']) {
            //  Now return the input type hidden with idproductattribute 
           return '<input type="hidden" name="id_product_attribute" id="product_attribute_info" value="'.$params['product']['id_product_attribute'].'"/>';
        }
    }
    

    Now on click of add to cart prevent the default action and fetch the idProductAttribute from the input hidden field.

    Login or Signup to reply.
  3. You could retrieve it after product has been updated, using Prestashop’s events

    //set scope variable to use in other parts of code
    var idProdAttr = 0;
    
    $(function(){
    
      // product has been updated
      prestashop.on("updatedProduct", function(ev){  
            
          idProdAttr = ev.id_product_attribute;
      });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search