WooCommerce Order Line Items

I an trying to get multiple line items and their metadata from WooCommerce orders.

I get the following JSON for an order:

"line_items": [
{
"id": 48,
"name": "Hoody",
"product_id": 96,
"variation_id": 97,
"quantity": 1,
"tax_class": "",
"subtotal": "20.00",
"subtotal_tax": "0.00",
"total": "20.00",
"total_tax": "0.00",
"taxes": [
],
"meta_data": [
{
"id": 465,
"key": "pa_size",
"value": "xl",
"display_key": "Size",
"display_value": "XL"
}
],
"sku": "",
"price": 20,
"parent_name": "Hoody"
},
{
"id": 49,
"name": "Polo",
"product_id": 94,
"variation_id": 95,
"quantity": 1,
"tax_class": "",
"subtotal": "12.00",
"subtotal_tax": "0.00",
"total": "12.00",
"total_tax": "0.00",
"taxes": [
],
"meta_data": [
{
"id": 475,
"key": "pa_colour",
"value": "white",
"display_key": "Colour",
"display_value": "White"
},
{
"id": 476,
"key": "pa_size",
"value": "m",
"display_key": "Size",
"display_value": "M"
}
],
"sku": "",
"price": 12,
"parent_name": "Polo"
},
{
"id": 50,
"name": "Polo",
"product_id": 94,
"variation_id": 95,
"quantity": 3,
"tax_class": "",
"subtotal": "36.00",
"subtotal_tax": "0.00",
"total": "36.00",
"total_tax": "0.00",
"taxes": [
],
"meta_data": [
{
"id": 486,
"key": "pa_colour",
"value": "red",
"display_key": "Colour",
"display_value": "Red"
},
{
"id": 487,
"key": "pa_size",
"value": "s",
"display_key": "Size",
"display_value": "S"
}
],
"sku": "",
"price": 12,
"parent_name": "Polo"
}
]

Each line item can have a number of meta_data entires: Size and Colour for example.

What I am trying to do is get the the following for all of the items in the order, for example with line_item: id 50:

name: Polo
meta_data:display_key: Colour
meta_data:value: Red
meta_data:display_key: Size
meta_data:value: S

Thanks!

Hi @seank1968, unfortunately the JSON you have provided is incomplete, so I don’t know the exact structure of the data you’re currently working with.

But assuming line_items is a top level field of your n8n items, you could first use the Item Lists node to split out line_items like so:

You can then use a second Item Lists node to split out the meta_data field for each line item and finally a Set node defining the exact fields you’d like to see, e.g. like so:

Example Workflow

@MutedJam yes, the line_items field is top level.

That worls great with the JSON example I used - but just tried it on our actual site with live data and realised there is a lot more meta_data being sent with each order!

This is line from it:


"line_items": [
{
"id": 17759,
"name": "Gymnastics Club Hoody",
"product_id": 40088,
"variation_id": 40100,
"quantity": 1,
"tax_class": "zero-rate",
"subtotal": "21.00",
"subtotal_tax": "0.00",
"total": "21.00",
"total_tax": "0.00",
"taxes": [
{
"id": 2,
"total": "0",
"subtotal": "0"
}
],
"meta_data": [
{
"id": 147627,
"key": "pa_size-gray",
"value": "5-6",
"display_key": "Size",
"display_value": "5-6"
},
{
"id": 147628,
"key": "pa_colour",
"value": "sapphire",
"display_key": "Colour",
"display_value": "Sapphire"
},
{
"id": 147629,
"key": "_tmcartepo_data",
"value": [
{
"mode": "builder",
"cssclass": "",
"hidelabelincart": "",
"hidevalueincart": "",
"hidelabelinorder": "",
"hidevalueinorder": "",
"element": {
"type": "textfield",
"rules": [
[...] // 1 items
],
"rules_type": [
[...] // 1 items
],
"_": {
"price_type": false
}
},
"name": "First Name or Initials (Optional)",
"value": "Mariah",
"price": "3",
"section": "5e6a58f3dd56c3.45255270",
"section_label": "First Name or Initials (Optional)",
"percentcurrenttotal": 0,
"fixedcurrenttotal": 0,
"currencies": [
],
"price_per_currency": {
"GBP": "3"
},
"quantity": 1
}
],
"display_key": "_tmcartepo_data",
"display_value": [
{
"mode": "builder",
"cssclass": "",
"hidelabelincart": "",
"hidevalueincart": "",
"hidelabelinorder": "",
"hidevalueinorder": "",
"element": {
"type": "textfield",
"rules": [
[...] // 1 items
],
"rules_type": [
[...] // 1 items
],
"_": {
"price_type": false
}
},
"name": "First Name or Initials (Optional)",
"value": "Mariah",
"price": "3",
"section": "5e6a58f3dd56c3.45255270",
"section_label": "First Name or Initials (Optional)",
"percentcurrenttotal": 0,
"fixedcurrenttotal": 0,
"currencies": [
],
"price_per_currency": {
"GBP": "3"
},
"quantity": 1
}
]
},
{
"id": 147630,
"key": "_tm_epo_product_original_price",
"value": [
"18"
],
"display_key": "_tm_epo_product_original_price",
"display_value": [
"18"
]
},
{
"id": 147631,
"key": "_tm_epo",
"value": [
1
],
"display_key": "_tm_epo",
"display_value": [
1
]
},
{
"id": 147632,
"key": "_tmdata",
"value": [
{
"tmcp_post_fields": {
"tmcp_textfield_0": "Mariah"
},
"product_id": 40088,
"per_product_pricing": true,
"cpf_product_price": "18",
"variation_id": "40100",
"form_prefix": "",
"tc_added_in_currency": "GBP",
"tc_default_currency": "GBP",
"tmcartepo_data": [
{
"key": "Mariah",
"attribute": "tmcp_textfield_0"
}
]
}
],
"display_key": "_tmdata",
"display_value": [
{
"tmcp_post_fields": {
"tmcp_textfield_0": "Mariah"
},
"product_id": 40088,
"per_product_pricing": true,
"cpf_product_price": "18",
"variation_id": "40100",
"form_prefix": "",
"tc_added_in_currency": "GBP",
"tc_default_currency": "GBP",
"tmcartepo_data": [
{
"key": "Mariah",
"attribute": "tmcp_textfield_0"
}
]
}
]
}
],
"sku": "rgc002",
"price": 21,
"parent_name": "Gymnastics Club Hoody"
}
], 

The Size and Colour lines come through great but how would I also get the ‘_tmcartepo_data’ entries (starting on line 38)

name: First Name or Initials (Optional) - line 59
value: Mariah - line 60

Also - can I exclude and other meta_data keys for example ‘_tm_epo_product_original_price’ on line 112?

Many thanks!

Hi @seank1968, this looks a bit tricky as some of the values in the meta_data:value field are arrays of different types while some other values are primitives. So you’d need different routes for processing if you want to do this in a no-code fashion which can get a rather tricky.

It might be easier here to use a Function node and a custom JS snippet in order to get the desired results.

Unfortunately the most recent example data you have shared doesn’t seem to be valid JSON. Would you be able to share the exact data structure you are currently working with, ideally copied straight from n8n’s JSON view by first selecting the outer most brackets and then copying the value like so:

It would be great if you could also confirm the exact format of the transformation you’d like to see using the JSON format. This way I should be able to help you build a suitable transformation Function.

@MutedJam thanks!

What I am trying to acheive is to add a new card into Trello for each new order in WooCommerce. Currently it is creating the card with the order date, order ID and customer name as the card name.
What I also want is to have each of the order line items appear in the card description.
So, in this case I am lookiing for something like:

1x Gymnastics Club Sleeveless Leotard Size: 26
1x Gymnastics Club Velour Shorts: Size 26
1x Gymnastics Club Hoody: Sapphire 5-6: First Name or Initials (Optional): Mariah

Size is coming from meta_data key ‘pa_size-gray’
Colour from key ‘pa_colour’
Name from key ‘_tmcartepo_data’

The full JSON from theorder is below.

Thanks for your help!

[
  {
    "id": 58858,
    "parent_id": 0,
    "status": "processing",
    "currency": "GBP",
    "version": "6.0.0",
    "prices_include_tax": true,
    "date_created": "2022-01-19T20:21:33",
    "date_modified": "2022-01-19T20:21:36",
    "discount_total": "0.00",
    "discount_tax": "0.00",
    "shipping_total": "5.42",
    "shipping_tax": "1.08",
    "cart_tax": "0.00",
    "total": "72.25",
    "total_tax": "1.08",
    "customer_id": 1073,
    "order_key": "wc_order_itBATxMTLztnF",
    "billing": {
      "first_name": "XXXXXXXXXXXX",
      "last_name": "XXXXXXXXXXXX",
      "company": "",
      "address_1": "XXXXXXXXXXXX",
      "address_2": "XXXXXXXXXXXX",
      "city": "XXXXXXXXXXXX",
      "state": "XXXXXXXXXXXX",
      "postcode": "XXXXXXXXXXXX",
      "country": "XXXXXXXXXXXX",
      "email": "XXXXXXXXXXXX",
      "phone": "XXXXXXXXXXXX"
    },
    "shipping": {
      "first_name": "XXXXXXXXXXXX",
      "last_name": "XXXXXXXXXXXX",
      "company": "",
      "address_1": "XXXXXXXXXXXX",
      "address_2": "XXXXXXXXXXXX",
      "city": "XXXXXXXXXXXX",
      "state": "XXXXXXXXXXXX",
      "postcode": "XXXXXXXXXXXX",
      "country": "XXXXXXXXXXXX",
      "phone": ""
    },
    "payment_method": "ppcp-gateway",
    "payment_method_title": "PayPal",
    "transaction_id": "XXXXXXXXXXXX",
    "customer_ip_address": "XXXXXXXXXXXX",
    "customer_user_agent": "MXXXXXXXXXXXX",
    "created_via": "checkout",
    "customer_note": "",
    "date_completed": null,
    "date_paid": "2022-01-19T20:21:36",
    "cart_hash": "XXXXXXXXXXXX",
    "number": "58858",
    "meta_data": [
      {
        "id": 4867213,
        "key": "is_vat_exempt",
        "value": "no"
      },
      {
        "id": 4867214,
        "key": "_ppcp_paypal_order_id",
        "value": "XXXXXXXXXXXX"
      },
      {
        "id": 4867215,
        "key": "_ppcp_paypal_intent",
        "value": "CAPTURE"
      },
      {
        "id": 4867216,
        "key": "_ppcp_paypal_payment_mode",
        "value": "live"
      },
      {
        "id": 4867224,
        "key": "_wcpdf_invoice_settings",
        "value": {
          "enabled": "1",
          "attach_to_email_ids": {
            "new_order": "1",
            "customer_processing_order": "1",
            "customer_invoice": "1"
          },
          "display_shipping_address": "1",
          "display_email": "1",
          "display_phone": "1",
          "display_date": "invoice_date",
          "display_number": "invoice_number",
          "number_format": {
            "prefix": "",
            "suffix": "",
            "padding": ""
          },
          "my_account_buttons": "available",
          "paper_size": "a4",
          "font_subsetting": false,
          "header_logo": "53787",
          "header_logo_height": "",
          "shop_name": {
            "default": "XXXXXXXXXXXX"
          },
          "shop_address": {
            "default": "XXXXXXXXXXXX"
          },
          "footer": {
            "default": ""
          },
          "extra_1": {
            "default": ""
          },
          "extra_2": {
            "default": ""
          },
          "extra_3": {
            "default": ""
          }
        }
      },
      {
        "id": 4867225,
        "key": "_wcpdf_invoice_date",
        "value": "1642623696"
      },
      {
        "id": 4867226,
        "key": "_wcpdf_invoice_date_formatted",
        "value": "2022-01-19 20:21:36"
      },
      {
        "id": 4867227,
        "key": "_wcpdf_invoice_number",
        "value": "5713"
      },
      {
        "id": 4867228,
        "key": "_wcpdf_invoice_number_data",
        "value": {
          "number": 5713,
          "formatted_number": "5713",
          "prefix": "",
          "suffix": "",
          "document_type": "invoice",
          "order_id": 58858,
          "padding": ""
        }
      },
      {
        "id": 4867229,
        "key": "_new_order_email_sent",
        "value": "true"
      },
      {
        "id": 4867230,
        "key": "_seopress_ga_tracked",
        "value": "1"
      }
    ],
    "line_items": [
      {
        "id": 17757,
        "name": "Gymnastics Club Sleeveless Leotard - 26",
        "product_id": 40968,
        "variation_id": 46878,
        "quantity": 1,
        "tax_class": "zero-rate",
        "subtotal": "27.25",
        "subtotal_tax": "0.00",
        "total": "27.25",
        "total_tax": "0.00",
        "taxes": [
          {
            "id": 2,
            "total": "0",
            "subtotal": "0"
          }
        ],
        "meta_data": [
          {
            "id": 147607,
            "key": "pa_size-gray",
            "value": "26",
            "display_key": "Size",
            "display_value": "26"
          }
        ],
        "sku": "rgc025",
        "price": 27.25,
        "parent_name": "Gymnastics Club Sleeveless Leotard"
      },
      {
        "id": 17758,
        "name": "Gymnastics Club Velour Shorts - 26",
        "product_id": 46895,
        "variation_id": 46901,
        "quantity": 1,
        "tax_class": "zero-rate",
        "subtotal": "17.50",
        "subtotal_tax": "0.00",
        "total": "17.50",
        "total_tax": "0.00",
        "taxes": [
          {
            "id": 2,
            "total": "0",
            "subtotal": "0"
          }
        ],
        "meta_data": [
          {
            "id": 147617,
            "key": "pa_size-gray",
            "value": "26",
            "display_key": "Size",
            "display_value": "26"
          }
        ],
        "sku": "rgc028",
        "price": 17.5,
        "parent_name": "Gymnastics Club Velour Shorts"
      },
      {
        "id": 17759,
        "name": "Gymnastics Club Hoody",
        "product_id": 40088,
        "variation_id": 40100,
        "quantity": 1,
        "tax_class": "zero-rate",
        "subtotal": "21.00",
        "subtotal_tax": "0.00",
        "total": "21.00",
        "total_tax": "0.00",
        "taxes": [
          {
            "id": 2,
            "total": "0",
            "subtotal": "0"
          }
        ],
        "meta_data": [
          {
            "id": 147627,
            "key": "pa_size-gray",
            "value": "5-6",
            "display_key": "Size",
            "display_value": "5-6"
          },
          {
            "id": 147628,
            "key": "pa_colour",
            "value": "sapphire",
            "display_key": "Colour",
            "display_value": "Sapphire"
          },
          {
            "id": 147629,
            "key": "_tmcartepo_data",
            "value": [
              {
                "mode": "builder",
                "cssclass": "",
                "hidelabelincart": "",
                "hidevalueincart": "",
                "hidelabelinorder": "",
                "hidevalueinorder": "",
                "element": {
                  "type": "textfield",
                  "rules": [
                    [
                      "3"
                    ]
                  ],
                  "rules_type": [
                    [
                      ""
                    ]
                  ],
                  "_": {
                    "price_type": false
                  }
                },
                "name": "First Name or Initials (Optional)",
                "value": "Mariah",
                "price": "3",
                "section": "5e6a58f3dd56c3.45255270",
                "section_label": "First Name or Initials (Optional)",
                "percentcurrenttotal": 0,
                "fixedcurrenttotal": 0,
                "currencies": [],
                "price_per_currency": {
                  "GBP": "3"
                },
                "quantity": 1
              }
            ],
            "display_key": "_tmcartepo_data",
            "display_value": [
              {
                "mode": "builder",
                "cssclass": "",
                "hidelabelincart": "",
                "hidevalueincart": "",
                "hidelabelinorder": "",
                "hidevalueinorder": "",
                "element": {
                  "type": "textfield",
                  "rules": [
                    [
                      "3"
                    ]
                  ],
                  "rules_type": [
                    [
                      ""
                    ]
                  ],
                  "_": {
                    "price_type": false
                  }
                },
                "name": "First Name or Initials (Optional)",
                "value": "Mariah",
                "price": "3",
                "section": "5e6a58f3dd56c3.45255270",
                "section_label": "First Name or Initials (Optional)",
                "percentcurrenttotal": 0,
                "fixedcurrenttotal": 0,
                "currencies": [],
                "price_per_currency": {
                  "GBP": "3"
                },
                "quantity": 1
              }
            ]
          },
          {
            "id": 147630,
            "key": "_tm_epo_product_original_price",
            "value": [
              "18"
            ],
            "display_key": "_tm_epo_product_original_price",
            "display_value": [
              "18"
            ]
          },
          {
            "id": 147631,
            "key": "_tm_epo",
            "value": [
              1
            ],
            "display_key": "_tm_epo",
            "display_value": [
              1
            ]
          },
          {
            "id": 147632,
            "key": "_tmdata",
            "value": [
              {
                "tmcp_post_fields": {
                  "tmcp_textfield_0": "Mariah"
                },
                "product_id": 40088,
                "per_product_pricing": true,
                "cpf_product_price": "18",
                "variation_id": "40100",
                "form_prefix": "",
                "tc_added_in_currency": "GBP",
                "tc_default_currency": "GBP",
                "tmcartepo_data": [
                  {
                    "key": "Mariah",
                    "attribute": "tmcp_textfield_0"
                  }
                ]
              }
            ],
            "display_key": "_tmdata",
            "display_value": [
              {
                "tmcp_post_fields": {
                  "tmcp_textfield_0": "Mariah"
                },
                "product_id": 40088,
                "per_product_pricing": true,
                "cpf_product_price": "18",
                "variation_id": "40100",
                "form_prefix": "",
                "tc_added_in_currency": "GBP",
                "tc_default_currency": "GBP",
                "tmcartepo_data": [
                  {
                    "key": "Mariah",
                    "attribute": "tmcp_textfield_0"
                  }
                ]
              }
            ]
          }
        ],
        "sku": "rgc002",
        "price": 21,
        "parent_name": "Retford Gymnastics Club Hoody"
      }
    ],
    "tax_lines": [
      {
        "id": 17761,
        "rate_code": "GB-ZERO VAT-1",
        "rate_id": 2,
        "label": "Zero VAT",
        "compound": false,
        "tax_total": "0.00",
        "shipping_tax_total": "0.00",
        "rate_percent": 0,
        "meta_data": []
      },
      {
        "id": 17762,
        "rate_code": "GB-VAT-1",
        "rate_id": 1,
        "label": "VAT",
        "compound": false,
        "tax_total": "0.00",
        "shipping_tax_total": "1.08",
        "rate_percent": 20,
        "meta_data": []
      }
    ],
    "shipping_lines": [
      {
        "id": 17760,
        "method_title": "Standard Delivery",
        "method_id": "oik_weight_zone_shipping",
        "instance_id": "6",
        "total": "5.42",
        "total_tax": "1.08",
        "taxes": [
          {
            "id": 1,
            "total": "1.084",
            "subtotal": ""
          }
        ],
        "meta_data": []
      }
    ],
    "fee_lines": [],
    "coupon_lines": [],
    "refunds": [],
    "date_created_gmt": "2022-01-19T20:21:33",
    "date_modified_gmt": "2022-01-19T20:21:36",
    "date_completed_gmt": null,
    "date_paid_gmt": "2022-01-19T20:21:36",
    "currency_symbol": "£",
    "_links": {
      "self": [
        {
          "href": ""
        }
      ],
      "collection": [
        {
          "href": ""
        }
      ],
      "customer": [
        {
          "href": ""
        }
      ]
    }
  }
]