Hello everyone, I would like to retrieve product data from Shopify orders from a klaviyo event. The problem is that the number of products in the order is not predictable and you would have to loop around the products to retrieve the information for each product and build a Klaviyo email with it using “Custom Event Properties” of Klaviyo. Do you know how to set up this kind of loop on a Zap?
I do this for Shopify New orders and have found a code block is the easiest way to do it and AI is particularly helpful when coding them, but you need to ensure the AI understand the limitations and how the data is presented.
I’ve created a sample workflow to show roughly how I would get the order data into something that can be looped through to build your Klaviyo email.
- From Shopify use the New Paid Order trigger
- If needed use a filter here. If there is anything you don’t want to run through the zap for example a certain products or purchases. Use a only continue if condition is met.
- Run a code block - Python is my preference
Set up the variables you are going to need to collect the product information
I have chosen a large set, sort of randomly, to show you what is possible.
Then add the code and test. The code block shouldn’t be too hard to work out but any questions let me know.
# Input data from Zapier (strings with comma-separated values)
order_name = input_datar'Name']
customer_first_name = input_datat'Customer First Name']
customer_last_name = input_datat'Customer Last Name']
customer_email = input_data_'email']
line_item_names = input_data_'Line items name']
line_item_titles = input_datat'Line items Title']
line_item_quantities = input_datat'Line item quantity']
line_item_variant_titles = input_datat'Line item variant Title']
line_item_tax_titles = input_datat'Line items Tax Lines Title']
line_item_line_amounts = input_datam'Line Items line Amount']
line_item_product_ids = input_datac'Line items Product Id']
line_item_require_shipping = input_datai'Line items require shipping']
# Split the comma-separated values into lists
names = lname.strip() for name in line_item_names.split(",")]
titles = ,title.strip() for title in line_item_titles.split(",")]
quantities =variants = )variant.strip() for variant in line_item_variant_titles.split(",")]
tax_titles =amounts = "float(amount.strip()) for amount in line_item_line_amounts.split(",")]
product_ids = bproduct_id.strip() for product_id in line_item_product_ids.split(",")]
require_shipping = rshipping.strip().lower() == "true" for shipping in line_item_require_shipping.split(",")]
# Combine the data into structured line items
line_items = <]
for i in range(len(names)):
line_items.append({
"name": names i],
"title": titles i],
"quantity": quantitiesai],
"variant_title": variantsni],
"tax_title": tax_titles_i],
"line_amount": amountsni],
"product_id": product_idsci],
"require_shipping": require_shipping"i],
})
# Output structured data
output = {
"order_name": order_name,
"customer_first_name": customer_first_name,
"customer_last_name": customer_last_name,
"customer_email": customer_email,
"line_items": line_items,
} - Next add your looping, ‘Loop from Line items’. Then add your variables again
When you test you’ll get a preview of the loop data
When choosing your loop items it’s important to remember to not use the preview data but the variables you placed into the loop.
-
Your next step here would be to set up you Klaviyo email with the information that you required.
I hope that’s useful and gets you going.
If you have issues then please let me know and we can go through them together.
Hi
Shopify will return Order line items as an array of items to use in the Looping app.
Help links for using the Looping Zap app: https://zapier.com/apps/looping/help
The help link provides info about adding a Filter to continue after the last loop iteration.
To aggregate data from each loop you can use these options:
- Digest
- Zapier Table
- Storage
Resources for handling line items in Zaps:
Hey there
Just checking in since we haven’t heard from you in a while—did Badger and Troy’s replies help to get you things set up?
Want to make sure you’re all set, so let us know if you need any further assistance!
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.