【问题标题】:Unable to get my product ids after constructing the handle in Shopify在 Shopify 中构建句柄后无法获取我的产品 ID
【发布时间】:2021-06-07 22:14:27
【问题描述】:

几小时以来,我一直在困扰一个问题。我希望有人能够理解我的问题并在可能的情况下提供帮助。我会尽量说清楚:

我需要在他们的收藏中显示我所有产品的所有评论。我的系列是这样构成的:它们只显示 PARENT 产品。举个例子,我所有父母的产品都是这样构造的:

父母的产品句柄:parenthandle

我所有的父母产品都有 2 个这样构造的子产品:

孩子的产品句柄 - tshirt man:parenthandle-tshirt-man

儿童产品句柄 - tshirt woman:parenthandle-tshirt-woman

为了能够显示我的父产品评论,我需要连接其孩子的产品 ID,因为只有我孩子的产品有评论(这就是我的评论应用程序的制作方式)。

所以,既然您已经了解了上下文,那么在我的收藏页面中,我构建了这样的内容:

{% assign producthandles_homme = "" %}
{% assign producthandles_femme = "" %}
{% assign id_enfant_homme = "" %}
{% assign id_enfant_femme = "" %}
{% assign allinone = "" %}

    {% for product in collection.products %}
       
        // Here I'm gonna construct my child products handle from their parents handle, to be able to then get their ids

        {% assign producthandles_homme =  producthandles_homme | append: product.handle | append: '-tshirt-homme' %}
        {% assign producthandles_femme =  producthandles_femme | append: product.handle | append: '-tshirt-femme' %} 

        // Now I can get their id as I constructed their handle

        {% assign id_enfant_homme = all_products[producthandles_homme].id | append: product.id | append: ',' %}
        {% assign id_enfant_femme = all_products[producthandles_femme].id | append: product.id | append: ',' %}

        // Here what I want to do is to concat all the ids of all my child product to be able to display their reviews

        {% assign allinone = allinone | append: id_enfant_homme | append: id_enfant_femme %}
       
    {% endfor %}

    // Finally here I want to display them all in "data-product-id"
    <div class="avis-verifies-etoiles-collection">
      <div class="NETREVIEWS_PRODUCT_STARS" data-product-id="{{ allinone }}"></div>
    </div>

不幸的是,它不起作用,我不明白为什么。有时它重复相同的 id,有时不重复,所有类型的 id 都在显示:

Here is the screenshot of the result

有人可以帮助我吗?我希望我是清楚的!

提前谢谢你!!! 科林

【问题讨论】:

    标签: for-loop shopify liquid


    【解决方案1】:

    尝试将您的 for 循环替换为以下内容:

    {%- liquid
      for product in collection.products
        assign producthandles_homme =  product.handle | append: '-tshirt-homme'
        assign producthandles_femme =  product.handle | append: '-tshirt-femme'
    
        if all_products[producthandles_homme] != blank
          assign allinone = allinone | append: ',' | append: all_products[producthandles_homme].id
        endif
    
        if all_products[producthandles_femme] != blank
          assign allinone = allinone | append: ',' | append: all_products[producthandles_femme].id
        endif
      endfor
    -%}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 2016-12-05
      • 2016-09-26
      • 2015-06-16
      相关资源
      最近更新 更多