Shopify 为现有变量赋值
目标是检查购物车中的某个项目,如果该项目存在,则将该项目的item.product.id值重新分配给另一个产品id 分配在下面的“我的代码”中不起作用。输出等于原始值Shopify 为现有变量赋值,shopify,liquid,Shopify,Liquid,目标是检查购物车中的某个项目,如果该项目存在,则将该项目的item.product.id值重新分配给另一个产品id 分配在下面的“我的代码”中不起作用。输出等于原始值 {% for item in cart.items %} {% if item.product.id == 4456879030362 %} {% assign item.product.id = 3962085671002 %} <div class="upsell-pop" style="text-al
{% for item in cart.items %}
{% if item.product.id == 4456879030362 %}
{% assign item.product.id = 3962085671002 %}
<div class="upsell-pop" style="text-align:center; width: 100%; border: 1px solid red;">
<h4>Frequently bought together</h4>
<p><a href="{{item.product.url}}">{{ item.product.title }}</a></p>
<img src="{{ item.image.src | product_img_url: 'medium' }}">
<span class="h3 price text-center">
{% if item.original_price > item.final_price %}
<s>{{ item.original_price | money }}</s>
{% endif %}
{{ item.final_price | money }}
</span>
<form action="/cart/add" data-productid="{{item.product.id}}" method="post">
<input type="hidden" name="id" data-productid="{{item.product.id}}" class="product-select" value="{{ item.product.variants[0].id }}" data-variant-title="{{ item.product.variants[0].title }}" />
<input type="submit" value="Add To Cart" />
</form>
</div>
{% endif %}
{% endfor %}
{%用于购物车中的项目。项目%}
{%if item.product.id==445687903062%}
{%assign item.product.id=3962085671002%}
经常一起买
{%if item.original_price>item.final_price%}
{{item.original|u price | money}}
{%endif%}
{{item.final|u price|money}}
{%endif%}
{%endfor%}
我不确定您是在尝试通过液体更新值(这是不可能的),还是在使用表单(无法按照您描述的方式工作)
1) 可以重新指定对象 这不是有效的代码
{%assign item.product.id=3962085671002%}
Assign用于创建或覆盖变量。您不能使用它来修改对象及其值
2) 当您更新购物车时,必须通过JS进行更新
如果您不想从购物车中交换特定产品,则必须使用/cart/update.js
结束,请参阅文档以获取参考:
3) 将产品添加到购物车时,必须使用其variant.id
Shopify中的所有产品都使用您购买的变体(即使它们没有变体,它们也有默认的隐藏变体)。因此,您必须传递variant.id
,而不是product.id
因此,当您想使用cart/update.js
交换特定产品时,必须以variant.id
为目标
那你该怎么办? 您仍然可以检查
{%if item.product.id==4456879030362%}
,但此时您有几个选项
1) 将特定类添加到项目中,并使用javascrip将该类作为目标,以便将项目与外部javascript文件中的新项目交换
2) 在IF语句中直接添加JS代码,并再次使用/cart/update.JS
交换产品
{% for item in cart.items %}
{% if item.product.id == 4456879030362 %}
<script>
jQuery.post('/cart/update.js',
"updates[{{item.id}}]=0&updates[{{YOUR_NEW_VARIANT_ID}}]=1"
);
</script>
...
{% endif %}
{% endfor %}
{%用于购物车中的项目。项目%}
{%if item.product.id==445687903062%}
jQuery.post('/cart/update.js',,
“更新[{item.id}}]=0&更新[{{YOUR_NEW_VARIANT_id}}]=1”
);
...
{%endif%}
{%endfor%}
如果我理解得很清楚,您尝试实现的是,如果购物车中有特定产品,则显示向购物车添加的追加销售表单
首先,正如drip所解释的,您不能重新分配数据库中记录的现有对象属性值。只能使用assign标记创建新变量
但好消息是你不需要做你想做的事
然后在Shopify中,对象的键是handle属性,而不是ID属性。例如,要获得特定产品,您可以通过其手柄:
{{ all_products['my-product-handle'].title }}
您还必须考虑,在以下情况下使用字符串时,需要添加引号:
{% if product.handle == 'foo' %}
因此为了实现您的目标,您可以尝试以下方法:
{% for item in cart.items %}
{% if item.product.handle == 'my-upsell-trigger-product-handle' %}
{% assign upsell_product = all_products['my-upsell-product-handle'] %}
{{ upsell_product.title }}
{% form "product", upsell_product %}
<input type="hidden" name="id" value="{{ upsell_product.selected_or_first_available_variant.id }}">
<input type="submit" value="Add to cart" />
{% endform %}
{% endif %}
{% endfor %}
{%用于购物车中的项目。项目%}
{%if item.product.handle=='my upsell触发器产品句柄'%}
{%assign upsell_product=所有_产品['my-upsell-product-handle']%}
{{upsell_product.title}}
{%form“product”,upsell_product%}
{%endform%}
{%endif%}
{%endfor%}
没有测试,但这应该工作
嗯
有用的文件:
处理:
基本操作员:
全局对象:
添加到购物车表单:您好,感谢您抽出时间回答我的问题。我试着复制你的代码并用3962085671002替换你的新变体ID。当我运行脚本时,它没有更新ID。它输出与以前相同的产品ID。此外,我不想更新购物车。这是一个小脚本,如果某人的购物车中有特定的产品,则向其显示追加销售的产品。