Technology 2 min read

Shopify + Zapier: How to Itemize Order Line Item Properties

Stick-figure comic: a messy blob of Shopify order data on the left, organized into neat itemized rows via Zapier on the right.

Zapier does not handle Shopify's order line item properties well. It gives you three fields to choose from:

  • Line Item Property Names
  • Line Item Property Values
  • Line Item Properties

All three are plain text (string) fields. Merging the names (keys) with the values sounds easy, but Zapier's built-in tools don't handle it. The sticking point is splitting comma-delimited text when a value has a comma in it, which customer-entered values often do.

We tried Zapier's Utilities steps with many combinations of options, and none of them gave us a clean key-value object back.

Solution

So we do it ourselves with a Code by Zapier step, with JavaScript selected as the language.

In the step's input data, add a variable (we called ours rawData) and map it to the raw Line Item Properties field. That field shows up as empty in the picker, but it does carry the data:

Then paste this into the code box:

const properties = {};

// Each property arrives as a "Name: ..." line and a "Value: ..." line,
// with a blank line between properties.
const items = (inputData.rawData || '').trim().split(/\n\s*\n/);

items.forEach(function (item) {
  const lines = item.split(/\n/);
  const name = lines[0].replace(/^Name:\s*/, '').trim();
  const value = lines.slice(1).join('\n').replace(/^Value:\s*/, '').trim();
  if (name) {
    properties[name] = value;
  }
});

output = properties;

The step returns one field per property, named after the property, so later steps can map each value directly.

How the code works

Zapier hands you the line item properties as one block of text, one "Name" line and one "Value" line per property, with a blank line between properties:

Name: Engraving text
Value: Love: always, forever

Name: Gift wrap
Value: Yes
const properties = {};

First, we create an empty object. An object lets us use named keys, where a regular array would only give us numbered positions.

const items = (inputData.rawData || '').trim().split(/\n\s*\n/);

Next, we take the raw text and split it on blank lines, so each piece holds one name and value pair. The || '' keeps the step from failing on an order with no properties.

const lines = item.split(/\n/);
const name = lines[0].replace(/^Name:\s*/, '').trim();
const value = lines.slice(1).join('\n').replace(/^Value:\s*/, '').trim();

For each pair, we split it into lines. The first line is the name and the rest is the value. We strip the leading "Name: " and "Value: " labels instead of splitting on the colon, so a value like "Love: always, forever" comes through whole. If a customer typed a line break into a field, the extra lines stay in the value too.

if (name) {
  properties[name] = value;
}

output = properties;

Finally, we add each pair to the object by name and return it. With the example above, the step outputs Engraving text = "Love: always, forever" and Gift wrap = "Yes", ready to use in any later step of your Zap.

Get the next one by email

One practical fix every two weeks for owners of established B2B companies. Websites, conversions and operations, written plainly.

Peter Krzyzek

eter Krzyzek is co-founder and CTO of Chykalophia, the Chicago agency that builds strategy-led websites for established B2B companies, and co-author of Made to Sell.

Comments

Read next

Keep going

All articles →

One practical fix, every two weeks

For owners who'd rather fix the leak than buy more traffic.