WooCommerce Line Items And meta_data

I’m trying to extract line items and the corresponding meta_data from WooCommere orders.

Sample JSON output:

{
"id": 60746,

"line_items": [
{
"id": 18637,
"name": "Retford CC Radial Short Sleeve Shirt - XL Youth",
"product_id": 58420,
"variation_id": 58431,
"quantity": 1,
"tax_class": "zero-rate",
"subtotal": "19.00",
"subtotal_tax": "0.00",
"total": "19.00",
"total_tax": "0.00",
"meta_data": [
{
"id": 155201,
"key": "pa_size-gray",
"value": "xl-youth",
"display_key": "Size",
"display_value": "XL Youth"
},
{
"id": 155202,
"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": "Initials",
"value": "CLSC",
"price": "2",
"section": "5afeda9983a2f6.17704414",
"section_label": "Initials",
"percentcurrenttotal": 0,
"fixedcurrenttotal": 0,
"currencies": [
],
"price_per_currency": {
"GBP": "2"
},
"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": "Initials",
"value": "CLSC",
"price": "2",
"section": "5afeda9983a2f6.17704414",
"section_label": "Initials",
"percentcurrenttotal": 0,
"fixedcurrenttotal": 0,
"currencies": [
],
"price_per_currency": {
"GBP": "2"
},
"quantity": 1
}
]
},
{
"id": 155203,
"key": "_tm_epo_product_original_price",
"value": [
"17"
],
"display_key": "_tm_epo_product_original_price",
"display_value": [
"17"
]
},
{
"id": 155204,
"key": "_tm_epo",
"value": [
1
],
"display_key": "_tm_epo",
"display_value": [
1
]
},
{
"id": 155205,
"key": "_tmdata",
"value": [
{
"tmcp_post_fields": {
"tmcp_textfield_0": "CLSC"
},
"product_id": 58420,
"per_product_pricing": true,
"cpf_product_price": "17",
"variation_id": "58431",
"form_prefix": "",
"tc_added_in_currency": "GBP",
"tc_default_currency": "GBP",
"tmcartepo_data": [
{
"key": "CLSC",
"attribute": "tmcp_textfield_0"
}
]
}
],
"display_key": "_tmdata",
"display_value": [
{
"tmcp_post_fields": {
"tmcp_textfield_0": "CLSC"
},
"product_id": 58420,
"per_product_pricing": true,
"cpf_product_price": "17",
"variation_id": "58431",
"form_prefix": "",
"tc_added_in_currency": "GBP",
"tc_default_currency": "GBP",
"tmcartepo_data": [
{
"key": "CLSC",
"attribute": "tmcp_textfield_0"
}
]
}
]
}
],
"sku": "ret304",
"price": 19,
"parent_name": "Retford CC Radial Short Sleeve Shirt"
},

],

"currency_symbol": "£"

}
] 

And a Function Node with:

const lineItems = items[0].json.line_items

const id = items[0].json.id

const _lineItems = [];

for (const [index, lineItem] of lineItems.entries()) {
  if (lineItem.meta_data[0].key === '_tmcartepo_data') {
      _lineItems.push({
        id: index + 1,
        name: lineItem.name,
        Item_notes: lineItem.meta_data[0].display_value
      })
  } else {
    let note = '';
    for (const meta of lineItem.meta_data) {
       note+= `${meta.display_key} ${meta.display_value} `
    }
    _lineItems.push({
        id: index + 1,
        name: lineItem.name,
        Item_notes: note
    })
  }
}

return [
  {
    json: {
      id,
      line_items: _lineItems
    }
  }
]

The result is:


[
{
"id": 60746,
"line_items": [
{
"id": 1,
"name": "Retford CC Radial Short Sleeve Shirt - XL Youth",
"Item_notes": "Size XL Youth _tmcartepo_data [object Object] _tm_epo_product_original_price 17 _tm_epo 1 _tmdata [object Object] "
},
{
"id": 2,
"name": "Retford CC Edge Pro Training Tee - Large Youth",
"Item_notes": "Size Large Youth _tmdata [object Object] "
},
{
"id": 3,
"name": "Retford CC Premuim Pro Hoody - Large Youth",
"Item_notes": "Size Large Youth _tmdata [object Object] "
}
]
}
] 

How do I get the data that is being returned as _tmcartepo_data [object Object] - this is the second meta_data item (ID: 155202) in the first line_item. I am trying to get the Name and value data - in this case ‘Initials’ and ‘CLSC’.

Thanks!

Hi @seank1968 I’m going to try to help you get started with JMESPath

First of all, I build valid JSON from your Sample output with https://jsonlint.com

{
	"id": 60746,

	"line_items": [{
			"id": 18637,
			"name": "Retford CC Radial Short Sleeve Shirt - XL Youth",
			"product_id": 58420,
			"variation_id": 58431,
			"quantity": 1,
			"tax_class": "zero-rate",
			"subtotal": "19.00",
			"subtotal_tax": "0.00",
			"total": "19.00",
			"total_tax": "0.00",
			"meta_data": [{
					"id": 155201,
					"key": "pa_size-gray",
					"value": "xl-youth",
					"display_key": "Size",
					"display_value": "XL Youth"
				},
				{
					"id": 155202,
					"key": "_tmcartepo_data",
					"value": [{
						"mode": "builder",
						"cssclass": "",
						"hidelabelincart": "",
						"hidevalueincart": "",
						"hidelabelinorder": "",
						"hidevalueinorder": "",
						"name": "Initials",
						"value": "CLSC",
						"price": "2",
						"section": "5afeda9983a2f6.17704414",
						"section_label": "Initials",
						"percentcurrenttotal": 0,
						"fixedcurrenttotal": 0,
						"currencies": [],
						"price_per_currency": {
							"GBP": "2"
						},
						"quantity": 1
					}],
					"display_key": "_tmcartepo_data",
					"display_value": [{
						"mode": "builder",
						"cssclass": "",
						"hidelabelincart": "",
						"hidevalueincart": "",
						"hidelabelinorder": "",
						"hidevalueinorder": "",
						"name": "Initials",
						"value": "CLSC",
						"price": "2",
						"section": "5afeda9983a2f6.17704414",
						"section_label": "Initials",
						"percentcurrenttotal": 0,
						"fixedcurrenttotal": 0,
						"currencies": [],
						"price_per_currency": {
							"GBP": "2"
						},
						"quantity": 1
					}]
				},
				{
					"id": 155203,
					"key": "_tm_epo_product_original_price",
					"value": [
						"17"
					],
					"display_key": "_tm_epo_product_original_price",
					"display_value": [
						"17"
					]
				},
				{
					"id": 155204,
					"key": "_tm_epo",
					"value": [
						1
					],
					"display_key": "_tm_epo",
					"display_value": [
						1
					]
				},
				{
					"id": 155205,
					"key": "_tmdata",
					"value": [{
						"tmcp_post_fields": {
							"tmcp_textfield_0": "CLSC"
						},
						"product_id": 58420,
						"per_product_pricing": true,
						"cpf_product_price": "17",
						"variation_id": "58431",
						"form_prefix": "",
						"tc_added_in_currency": "GBP",
						"tc_default_currency": "GBP",
						"tmcartepo_data": [{
							"key": "CLSC",
							"attribute": "tmcp_textfield_0"
						}]
					}],
					"display_key": "_tmdata",
					"display_value": [{
						"tmcp_post_fields": {
							"tmcp_textfield_0": "CLSC"
						},
						"product_id": 58420,
						"per_product_pricing": true,
						"cpf_product_price": "17",
						"variation_id": "58431",
						"form_prefix": "",
						"tc_added_in_currency": "GBP",
						"tc_default_currency": "GBP",
						"tmcartepo_data": [{
							"key": "CLSC",
							"attribute": "tmcp_textfield_0"
						}]
					}]
				}
			],
			"sku": "ret304",
			"price": 19,
			"parent_name": "Retford CC Radial Short Sleeve Shirt"
		}

	],

	"currency_symbol": "£"

}

Then I copied this JSON in a JMESPath example at JMESPath Examples — JMESPath

And then I started building the query:

line_items[*].meta_data[?key=='_tmcartepo_data'].[value]

I hope this is what you’re looking for and that this is helpful for you.

Enjoy n8n’ing and your weekend!

I also found a JMESPath Query tool in https://jsonparser.org

I :heart: JMESPath …

1 Like

Thanks - that’s helped a lot! Thanks also for the links - looks like they will be very useful!