Zapier 操作代码:Python input_data[] 从列表中去除空值

问题描述 投票:0回答:2

我一直在尝试使用 Python 为 Zapier 创建自定义代码。

代码从 Magento 2 发票中提取两个列表。它们是行项目的详细信息,我们使用这些数据来更新库存系统上的库存。不幸的是,捆绑产品显示了子产品,我需要将子产品的数量清零,这样它们就不会从库存中删除。

如果父级是“捆绑包”,我已将逻辑全部排序以将库存商品数量设置为零。

问题在于提取输入数据。空值正在被丢弃。

例如,如果列表为 null、null、null、bundle,则结果只是bundle 如果列表是 1,1,1,null 我最终得到的是 1,1,1

有什么方法可以从输入数据字段中提取数据而不删除空值吗?

代码目前看起来像这样。

# if the product is a child of a bundle then zero out the quantity or it will take extra stock

quantity = str(input_data["item_qty_invoiced"])
quantity_array = quantity.split(",")

cleaned_quantity_list = ""

product_type = str(input_data["item_product_type"])
product_type_array = product_type.split(",")

num_of_line_items = len(product_type_array)
index = 0

while index < num_of_line_items:

    if product_type_array[index] == "bundle":
        quantity_array[index] = 0

    index += 1

cleaned_quantity_list = ",".join(str(i) for i in quantity_array)    

return {'item_qty_invoiced': cleaned_quantity_list}

我还没有尝试过 javascript,但我很高兴看看它是否是一个选项。

python magento zapier
2个回答
0
投票

根据 Zapier 开发人员 2019 年 3 月的answer,输入强制转换为字符串的方式已修复,并且没有计划解决此问题。

一个建议的解决方法是:

最好的办法是制作一个小 CLI 应用程序来复制 twitter 操作。然后你可以将输出设置为json字符串,我们不会碰它。我已经在几个地方做到了这一点,而且效果很好(除了我们应该为您处理的时候必须自己与 Twitter 交互的额外负担之外)

但这确实违背了使用 Zapier 的一半目的。


0
投票

使用原始 webhook 就可以做到这一点。

然后定义一个输入

然后使用 json 模块加载它

import json
raw = json.loads(input_data["raw"])
lang = raw["payload"]["lang"]

output = [{
  'lang': lang,}]

另请参阅https://stackoverflow.com/a/55478782/1193450

© www.soinside.com 2019 - 2024. All rights reserved.