简体   繁体   English

PrestaShop 1.7 如何获取产品页面上的 id_product_attribute?

[英]PrestaShop 1.7 How to get id_product_attribute on product page?

id_product_attribute is available in URL - value "10": http://localhost/presta/women/2-10-brown-(...).html#/2-size-m id_product_attribute 在 URL 中可用 - 值“10”: http://localhost/presta/women/2-10-brown-(...).html#/2-size-m

I need to get current id_product_attribute from current product page .我需要从当前产品页面获取当前 id_product_attribute 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")可能来自 $_GET,或来自 DOM 元素,或 Presta 商店变量 - 但我必须将它传递给 JavaScript function,然后再添加到购物车(即使最终客户不将产品添加到购物车 - 这就是为什么我可以'使用钩子:“actionCartSave”)

I have access to this value from hook displayAfterProductThumbs - but is problem with getting current value.我可以从钩子 displayAfterProductThumbs 访问这个值 - 但是获取当前值有问题。 To get correct value, I need to:为了获得正确的价值,我需要:

1) choose product attributes on product page (size, color) 1)在产品页面选择产品属性(尺码、颜色)

2) refresh page to trigger hook "displayAfterProductThumbs" 2)刷新页面触发钩子“displayAfterProductThumbs”

3) read data 3)读取数据

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 .试图找到短语: id_product_attributeid_combinationidCombinationipa 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 的信息(在 Google 上找到)都与 SEO 相关,“为了 SEO 目的,在 url 中使用 id_product_attribute 不是个好主意”。

There is a hook called displayProductAdditionalInfo.有一个名为 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.因此 idProductAttribute 将在组合更改时自动更新。

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.现在单击添加到购物车可防止默认操作并从输入隐藏字段中获取 idProductAttribute。

Hi @DamianSobkowiak and welcome to SO:-)嗨@DamianSobkowiak,欢迎来到 SO:-)

In PrestaShop 1.6.x and older versions, you could retrieve this ID by using the idProductAttribute global JS variable.在 PrestaShop 1.6.x 及更早版本中,您可以使用idProductAttribute全局 JS 变量检索此 ID。

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.在 PrestaShop 1.7.x 版本中,买家选择的产品属性(尺寸、颜色等)ID 存储在数组中的group变量中,但此变量不再包含相关的id_product_attribute本身。

When a product is added to cart, the /controllers/front/CartController.php file is called and you can see the following on line 366:将产品添加到购物车时,将调用/controllers/front/CartController.php文件,您可以在第 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)在“添加到购物车”按钮上使用事件侦听器将 .js 文件添加到您的主题(使用event.preventDefault()以确保您有时间处理后续步骤)
  2. In case this event is triggered, make an Ajax call to a controller file that you will create with the following code:如果触发此事件,请调用 Ajax 调用您将使用以下代码创建的 controller 文件:

    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.拨打 ajax 时,不要忘记传递groupid_product值。

  3. Retrieve the result of the ajax call and store the id_product_attribute into a variable检索 ajax 调用的结果并将 id_product_attribute 存储到变量中

I hope this helps!我希望这有帮助!

You could retrieve it after product has been updated, using Prestashop's events您可以在产品更新后使用 Prestashop 的事件检索它

//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;
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM