我们正在使用本文作为参考,将搜索自动填充功能添加到Shopify商店中 . 搜索自动完成功能在不对其示例代码进行任何更改时按预期工作 .
我们的商店支持零售和批发客户 . 我们的产品在我们的商店中存在两次,根据您是零售还是批发,价格不同 . 如果我们使用基本示例代码,则复制产品会出现在搜索自动完成预览区域中,该区域包含客户不应看到的零售或批发 .
解决方案是过滤掉search.json.liquid创建的搜索结果对象 . 以下是上文中的基本示例代码 .
我相信我已正确修改代码,只显示零售(wholesale_customer = false,wholesale_product = false)或批发 . 我们遇到的问题是应该呈现产品json的最后一段代码没有正确验证或执行 . 我在产品json中添加了一些额外的行以进行调试,并将液体条件设置为{%if show_item%},因此它始终呈现产品 . batch_product和wholesale_customer布尔值总是设置正确,所以我不明白为什么show_item标志没有验证 .
{% assign lowercase_tags = customer.tags | downcase %}
{% assign wholesale_customer = false %}
{% if lowercase_tags contains 'wholesale' %}
{% assign wholesale_customer = true %}
{% endif %}
{% layout none %}
{% capture results %}
{% for item in search.results %}
{% assign wholesale_product = false %}
{% assign show_item = nil %}
{% for product_collection in item.collections %}
{% if product_collection.title == 'Wholesale' %}
{% assign wholesale_product = true %}
{% endif %}
{% endfor %}
{% if wholesale_customer == true and wholesale_product == true %}
{% assign show_item = true %}
{% elsif wholesale_customer == false and wholesale_product == false %}
{% assign show_item = true %}
{% endif %}
{% if show_item == true %}
{
"title": {{ item.title | json }},
"url": {{ item.url | within: item.collections.last | json }},
"thumbnail": {{ item.featured_image.src | product_img_url: 'thumb' | json }},
"wholesale_customer": {{ wholesale_customer | json }},
"wholesale_product": {{ wholesale_product | json }}
}
{% endif %}
{% unless forloop.last %},{% endunless %}
{% endfor %}
{% endcapture %}
{
"results_count": {{ search.results_count }},
"results": [{{ results }}]
}
我已经尝试在主if语句中放置一个调试变量来控制show_item是true还是false来测试条件是否正确触发,并且它们似乎正在工作 .
这是我如何设置调试变量来测试if语句是否正常工作 . 当我在搜索栏中输入内容时,我只是查看开发人员控制台,我在哪里控制台 . 记录json对象 .
{% if wholesale_customer == true and wholesale_product == true %}
{% assign show_item = true %}
{% assign debug = "wholesale_customer" %}
{% elsif wholesale_customer == false and wholesale_product == false %}
{% assign debug = "retail_customer" %}
% assign show_item = true %}
{% endif %}
{% if show_item == true %}
{
"title": {{ item.title | json }},
"url": {{ item.url | within: item.collections.last | json }},
"thumbnail": {{ item.featured_image.src | product_img_url: 'thumb' | json }},
"wholesale_customer": {{ wholesale_customer | json }},
"wholesale_product": {{ wholesale_product | json }},
"debug": {{ debug | json }}
}
{% endif %}