Shopify购物车属性多值留存问题:如何保存所有收件人信息至订单
解决Shopify购物车多商品收件人信息覆盖问题
问题根源很明确:你给所有商品的收件人姓名、邮箱用了完全相同的购物车属性键(Recipient first and last name、Recipient email),而Shopify购物车属性是键值对结构,相同键的后输入值会直接覆盖前一个,导致只有最后一组信息留存。
要实现所有商品的信息都保存,核心是给每个商品的属性字段生成唯一的键名,利用购物车商品自带的唯一标识或者循环索引即可动态实现,不需要提前固定数量。
修改后的代码示例
替换你当前的输入字段代码,用商品的item.key(每个购物车商品的唯一标识符)作为属性键的后缀:
<td class="medium-hide"> {{ item.product.title }} <p class="cart-attribute__field"> <label for="recipient-name-{{ item.key }}">First and last name</label> <input id="recipient-name-{{ item.key }}" type="text" name="attributes[Recipient Name - {{ item.key }}]" value="{{ cart.attributes['Recipient Name - ' + item.key] | escape }}" > </p> </td> <td class="cart-item__totals right small-hide"> <p class="cart-attribute__field"> <label for="recipient-email-{{ item.key }}">Recipient email</label> <input id="recipient-email-{{ item.key }}" type="text" name="attributes[Recipient Email - {{ item.key }}]" value="{{ cart.attributes['Recipient Email - ' + item.key] | escape }}" > </p> </td>
关键改动说明
- 唯一键名:用
item.key拼接属性名,生成Recipient Name - xxx、Recipient Email - xxx这样的唯一键,每个商品的属性不会冲突 - 唯一ID:input的id也加上
item.key,避免页面内id重复导致的标签关联问题 - 值回显:value属性对应到新的唯一键,确保购物车刷新后用户输入的信息不会丢失
订单中展示所有信息
订单后台会自动保存所有购物车属性,你可以在订单详情模板(比如order.liquid)中遍历所有属性,或者针对性输出:
{% for attribute in cart.attributes %} {% if attribute.first contains 'Recipient Name' or attribute.first contains 'Recipient Email' %} <p>{{ attribute.first }}: {{ attribute.second }}</p> {% endif %} {% endfor %}
这样就能在订单里看到每个商品对应的收件人姓名和邮箱了。
内容的提问来源于stack exchange,提问作者Omi in a hellcat
相关产品推荐
相关产品推荐

