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_bination , 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".
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_bination , 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".
Share Improve this question asked May 9, 2019 at 14:06 Damian SobkowiakDamian Sobkowiak 251 silver badge5 bronze badges3 Answers
Reset to default 3There 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 bination of the product. So the idProductAttribute will get updated automatically on change of bination.
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.
Hi @DamianSobkowiak and wele 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:
- 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)
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!
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;
});
});