【发布时间】:2020-04-09 11:03:42
【问题描述】:
我一直在尝试使用 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,但如果可以的话,我很高兴看到它。
【问题讨论】:
-
我也有这个问题。我有一个包含关键字 X 的键的输入,我想要它的值。我事先不知道确切的键,但我希望能够遍历所有键,找到哪个键包含关键字 X,并访问相关值。现在,我不能可靠地做到这一点,因为空值会被丢弃。