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
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 relatedid_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:A solution for you could be to:
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
andid_product
values when making your ajax call.Retrieve the result of the ajax call and store the id_product_attribute into a variable
I hope this helps!
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.
Now on click of add to cart prevent the default action and fetch the idProductAttribute from the input hidden field.
You could retrieve it after product has been updated, using Prestashop’s events