我一直在尝试使用 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,但我很高兴看看它是否是一个选项。
然后使用 json 模块加载它
import json
raw = json.loads(input_data["raw"])
lang = raw["payload"]["lang"]
output = [{
'lang': lang,}]