【问题标题】:Storing product line_items in an associative array with vendors将 product line_items 存储在与供应商的关联数组中
【发布时间】:2021-04-21 08:50:16
【问题描述】:

在生成“订单确认”电子邮件时,我想要一个根据供应商按“数组”排序的产品列表。

但我不知道如何设置产品线项目以与那里的供应商关联。

这是我正在尝试的;

{% assign vendor_count = 0 %}
{% capture vendor_check %}
    {% for line in line_items %}
        {{ line.product.vendor }}{% if forloop.last != true %},{% endif %}
    {% endfor %}
{% endcapture %}

{% assign line_vendor = vendor_check | split: ',' | uniq %}

{%- assign vendor_items = line_items | where: "vendor", vendor -%}

{% for vendor in line_vendor %}
      {% assign vendor_count = vendor_count | plus: 1 %}
      <div><strong>Delivery {{ vendor_count }} - {{ vendor }}</strong></div>
      {% for line in vendor_items %}
          {{ line.title }}<br>      
      {% endfor %}  
{% endfor %}

电流输出:

交付 1 - 供应商#1
产品1
产品2
产品3
产品 4

交付 2 - 供应商#2
产品1
产品2
产品3
产品 4

所需的输出:

交付 1 - 供应商#1
产品1
产品 2

交付 2 - 供应商#2
产品3
产品 4

我在这里做错了什么?

【问题讨论】:

    标签: arrays shopify liquid shopify-template shopify-api


    【解决方案1】:

    具有缩进的液体代码可能会通过插入意外的空格来混淆捕获的变量,要么将整个捕获标记放在 1 行上,要么转义空格。

    vendor_items 并不是真正需要的,只需从line_items 中创建一组独特的供​​应商,然后再次遍历您的line_items 并显示匹配的产品。

    您可以尝试这样的方法并根据您的需要进行调整:

    {% capture vendor_check %}{% for line in line_items %}{{ line.product.vendor }}{% if forloop.last != true %},{% endif %}{% endfor %}{% endcapture %}
    
    {% assign line_vendor = vendor_check | split: "," | uniq %}
    
    {% for vendor in line_vendor %}
          {% assign vendor_count = vendor_count | plus: 1 %}
          <div><strong>Delivery {{ vendor_count }} - {{ vendor }}</strong></div>
    
          {% for line in line_items %}
              {% if line.product.vendor == vendor %}{{ line.title }}{% endif %}<br>
          {% endfor %}  
    {% endfor %}
    

    【讨论】:

    • 这正是我所需要的,非常感谢!
    猜你喜欢
    • 2014-06-30
    • 2012-09-28
    • 1970-01-01
    • 2011-08-18
    • 2022-06-11
    • 1970-01-01
    • 2021-12-15
    • 2011-03-21
    • 2013-12-15
    相关资源
    最近更新 更多