问题
I have been trying to create custom code for Zapier using Python.
The code pulls in two lists from a Magento 2 invoice. They are details from the line items, we use the data to update inventory on our stock system. The unfortunate thing is bundled products display the child products and I need to zero out the quantity for the child products so they don't get removed from stock as well.
I have the logic all sorted to set the stock items quantity to zero if the parent is a "bundle".
The problem is pulling the input data. Nulls are being dropped.
Eg if the list is null, null, null, bundle the result is just bundle if the list is 1,1,1,null all I end up with is 1,1,1
Is there any way to pull the data from Input Data fields without removing the null values?
The code looks like this at the moment.
# 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}
I haven't tried javascript just yet, but am happy to look at that if it's an option.
回答1:
According to a March 2019 answer by a Zapier dev, the way inputs are coerced into strings is fixed, and there were no plans to resolve this issue.
One suggested workaround was:
the best thing to do is make a little CLI app that replicates the twitter action. Then you can set the output to be a json string and we won't touch it. I've done this in a couple of places and it works great (outside the added burden of having to interface with twitter yourself when we should take care of that for you)
This does defeat half the purpose of using Zapier though.
来源:https://stackoverflow.com/questions/59365846/zapier-action-code-python-input-data-stripping-nulls-from-list