Skip to main content
Question

Loop around products of an order Shopify to build a Klaviyo email

  • November 25, 2024
  • 3 replies
  • 49 views

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?
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

3 replies

Badger
Forum|alt.badge.img+5
  • New
  • 134 replies
  • November 25, 2024

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. 

 

  1. From Shopify use the New Paid Order trigger
     
  2. 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.
     
  3. 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_data['Name']  
    customer_first_name = input_data['Customer First Name'] 
    customer_last_name = input_data['Customer Last Name']  
    customer_email = input_data['email']  
    
    line_item_names = input_data['Line items name']  
    line_item_titles = input_data['Line items Title']
    line_item_quantities = input_data['Line item quantity']  
    line_item_variant_titles = input_data['Line item variant Title']  
    line_item_tax_titles = input_data['Line items Tax Lines Title']  
    line_item_line_amounts = input_data['Line Items line Amount']  
    line_item_product_ids = input_data['Line items Product Id']  
    line_item_require_shipping = input_data['Line items require shipping']  
    
    # Split the comma-separated values into lists
    names = [name.strip() for name in line_item_names.split(",")]
    titles = [title.strip() for title in line_item_titles.split(",")]
    quantities = [int(qty.strip()) for qty in line_item_quantities.split(",")]
    variants = [variant.strip() for variant in line_item_variant_titles.split(",")]
    tax_titles = [tax.strip() for tax in line_item_tax_titles.split(",")]
    amounts = [float(amount.strip()) for amount in line_item_line_amounts.split(",")]
    product_ids = [product_id.strip() for product_id in line_item_product_ids.split(",")]
    require_shipping = [shipping.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": quantities[i],
            "variant_title": variants[i],
            "tax_title": tax_titles[i],
            "line_amount": amounts[i],
            "product_id": product_ids[i],
            "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,
    }
    
  4. 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.

     

     

  5. 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. 


Troy Tessalone
Forum|alt.badge.img+14
  • Zapier Expert
  • 30959 replies
  • November 25, 2024

Hi ​@appareldistribution 

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:

 

Resources for handling line items in Zaps:

 

 

 


SamB
Community Manager
Forum|alt.badge.img+11
  • Community Manager
  • 7487 replies
  • December 2, 2024

Hey there ​@appareldistribution! 👋

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!