发送单个 API 请求给多个用户,每个收件人根据其 external_id 看到个性化内容。工作原理:
将 custom_data 构造为一个对象,其中键是 external_id,值是用户特定的数据
在模板中,使用 subscription.external_id 查找当前收件人的数据
OneSignal 为每个收件人使用其特定数据渲染一次模板
模板:
{% assign user = message.custom_data.users[subscription.external_id] %}Hi {{ user.first_name }}, you have {{ user.points }} points. Your level is {{ user.level }}.
{% assign cart = message.custom_data.cart %}{% assign item_count = cart.size | plus: 0 %}{% if item_count == 1 %}We're holding onto {{item_count}} item in your cart, but don't wait too long, other squirrels are getting ahead!{% endif %}{% if item_count > 1 %}We're holding onto {{item_count}} items in your cart, but don't wait too long, other squirrels are getting ahead!{% endif %}
4
开始循环
使用 for 循环为购物车中的每个商品重复产品展示行。在第 2 行(循环开始),将以下内容添加到文本块中:
Text
{% for product in message.custom_data.cart %}
这段代码的作用:
开始一个循环,遍历 cart 数组中的每个对象
创建一个名为 product 的临时变量,代表当前项目
{% for %} 和 {% endfor %} 之间的所有内容会为每个购物车项目重复一次
您可以将 product 命名为任何名称(例如 item、cartItem)——保持一致即可
for 循环的位置:确保 {% for %} 语法位于其单独的文本块行中。不要将其放在包含其他内容的多列行中,因为这可能会导致某些邮件客户端的渲染问题。
5
显示产品详情
这个 4 列行展示图片、名称、数量和价格。由于它在循环内,会为每个购物车商品重复。在第 3 行(产品详情),配置:第 1 列 - HTML 块(产品图片):
{% assign cart = message.custom_data.cart %}{% assign item_count = cart.size | plus: 0 %}{% if item_count == 1 %}You left {{cart.first.product_name}} in your cart.{% endif %}{% if item_count == 2 %}You left {{cart.first.product_name}} and {{item_count | minus: 1}} more item in your cart.{% endif %}{% if item_count > 2 %}You left {{cart.first.product_name}} and {{item_count | minus: 1}} more items in your cart.{% endif %}