检查特定产品是否在我的购物车中

Check If specific product is in my Cart

我创建了一个脚本来检查特定产品是否在我的购物车中以执行某些操作但无法正常工作...如果有人能告诉我我的代码中有什么问题将会非常有帮助...

{% for item in cart.items %}

{% if item.product.id == "8223550921" %}
    <div><h1>Some text here!</h1></div><br> 

{% endif %}

{% endfor %}

注意:我使用 product.title 和许多其他选项测试了很多时间,但都不起作用。

你可以试试这个

{% if item.product_id contains "8223550921" %}

检查 this cheat 以进一步使用订单项属性

由于产品 ID 是整数,因此您应该将其用作(从产品 ID 中删除引号):-

{% for item in cart.items %}
    {% if item.product.id == 8223550921 %}
        <div><h1>Some text here!</h1></div><br> 
    {% endif %} 
{% endfor %}

我自己找到了解决办法。关键是我使用了错误的对象。我正在使用购物车对象,但我正在结帐页面中工作,所以请查看解决方案:

{% for item in checkout.line_items %}

感谢您的帮助! - 亚伯.