Shopify 购物车中的动态属性名称

Dynamic Attribute Names in Shopify Cart

我对此非常了解,但我就是无法正确理解语法。几天来我一直在搞乱这个问题。本质上,我有一条贺卡消息,我希望客户在结帐页面 (cart.liquid) 上为每张贺卡填写消息,并且我需要为每个索引更改属性名称。因此,我将索引添加到每个属性名称,但无济于事。出于测试目的,这里有一个基本输入字段:

    <p class="cart-attribute__field" style="min-width:300px;">
      <label for="to{{ forloop.index }}">To:</label>
      <input class="checkMe" id="to{{ forloop.index  }}" type="text" name="attributes[To{{ forloop.index  }}]"  maxlength="40" data-stk="{{item.id}}" value="{{ cart.attributes['To'+forloop.index] }}" >

    </p> 

这部分 (value="{{ cart.attributes['To'+forloop.index] }}") 给我带来了麻烦。

您不能在 liquid 代码中使用“+”运算符进行追加。试试这个:

{% assign cart_attr = 'To' | append: forloop.index %}
<p class="cart-attribute__field" style="min-width:300px;">
<label for="to{{ forloop.index }}">To:</label>
<input class="checkMe" id="to{{ forloop.index  }}" type="text" name="attributes[To{{ forloop.index  }}]"  maxlength="40" data-stk="{{item.id}}" value="{{ cart.attributes[cart_attr] }}" >
</p>

注:根据需要即兴创作。

您为什么不直接使用 https://docs.shopify.com/manual/configuration/store-customization/page-specific/product-page/get-customization-information-for-products

中所述的订单项属性