You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Liquid中按产品类型获取列表并实现浏览召回邮件关联产品展示

Problem Diagnosis

The original code has two critical issues preventing it from functioning as required:

  1. Wrong product selection: Uses abandoned_products.first to fetch the earliest viewed product instead of the last one you need.
  2. Invalid filtering logic: Liquid's where filter only supports equality checks, so where: 'id', '!=', abandoned_product.id can't exclude the original product from the related list.
Fixed Code
{% assign abandoned_products = abandoned_visit.products_viewed %}
{% assign abandoned_product = abandoned_products.last %} {% comment %} Get the most recently viewed product {% endcomment %}
{% if abandoned_product %}
    <!-- Display the abandoned product -->
    <h2>You were looking at:</h2>
    <div>
        <a href="{{ abandoned_product.url }}">
            <img src="{{ abandoned_product.featured_image | img_url: 'medium' }}" alt="{{ abandoned_product.title }}">
            <p>{{ abandoned_product.title }}</p>
        </a>
        <p>{{ abandoned_product.price | money }}</p>
    </div>

    <!-- Fetch and display 3 more products from the same product type -->
    {% assign product_type = abandoned_product.type %}
    {% if product_type != blank %} {% comment %} Skip if product has no type set {% endcomment %}
        {% assign related_products = all_products | where: 'product_type', product_type | reject: 'id', abandoned_product.id | limit: 3 %}
        {% if related_products.size > 0 %}
            <h2>You might also like:</h2>
            <div>
                {% for product in related_products %}
                <div style="display: inline-block; margin: 10px;">
                    <a href="{{ product.url }}">
                        <img src="{{ product.featured_image | img_url: 'medium' }}" alt="{{ product.title }}">
                        <p>{{ product.title }}</p>
                    </a>
                    <p>{{ product.price | money }}</p>
                </div>
                {% endfor %}
            </div>
        {% endif %}
    {% endif %}
{% endif %}
Key Fixes Breakdown
  • Target last viewed product: Switched abandoned_products.first to abandoned_products.last to pull the most recent item the user browsed.
  • Valid product exclusion: Replaced the invalid inequality where check with reject: 'id', abandoned_product.id—this correctly removes the original product from the related results.
  • Type existence check: Added {% if product_type != blank %} to avoid running unnecessary logic if the abandoned product has no type assigned.

内容的提问来源于stack exchange,提问作者dan k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 10:02:21