Extract Data from WooCommerce metadata

I have included the Complete JSON output of WooCommerce Trigger.

How can I extract the values of _additional_wooccm1 & _additional_wooccm2 & _additional_wooccm3 with Function Node?

Currently using this code in a function.

But it picks the _woocs_order_rate if _additional_wooccm3 is not present

let index = 0;
for (const data of $node["WooCommerce Trigger"].json["meta_data"]) {
  items[0].json[`url${index++}`] = data.value.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0];
}
return items;

Complete JSON:

"-"{
   "id":136,
   "parent_id":0,
   "number":"136",
   "order_key":"wc_order_hJfDPpAfrYzix",
   "created_via":"checkout",
   "version":"4.7.0",
   "status":"processing",
   "currency":"USD",
   "date_created":"2020-11-17T20:08:24",
   "date_created_gmt":"2020-11-17T20:08:24",
   "date_modified":"2020-11-17T20:08:24",
   "date_modified_gmt":"2020-11-17T20:08:24",
   "discount_total":"19.95",
   "discount_tax":"0.00",
   "shipping_total":"0.00",
   "shipping_tax":"0.00",
   "cart_tax":"0.00",
   "total":"0.00",
   "total_tax":"0.00",
   "prices_include_tax":false,
   "customer_id":2,
   "customer_ip_address":"13.197.35.875",
   "customer_user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36",
   "customer_note":"",
   "-""billing":{
      "first_name":"MC",
      "last_name":"Naveen",
      "company":"",
      "address_1":"",
      "address_2":"",
      "city":"",
      "state":"",
      "postcode":"",
      "country":"",
      "email":"[email protected]",
      "phone":"98587"
   },
   "-""shipping":{
      "first_name":"",
      "last_name":"",
      "company":"",
      "address_1":"",
      "address_2":"",
      "city":"",
      "state":"",
      "postcode":"",
      "country":""
   },
   "payment_method":"",
   "payment_method_title":"",
   "transaction_id":"",
   "date_paid":"2020-11-17T20:08:24",
   "date_paid_gmt":"2020-11-17T20:08:24",
   "date_completed":null,
   "date_completed_gmt":null,
   "cart_hash":"69b3f066cae38b21b629be535116d8d3",
   "-""meta_data":[
      "-"{
         "id":1121,
         "key":"is_vat_exempt",
         "value":"no"
      },
      "-"{
         "id":1122,
         "key":"_additional_wooccm1",
         "value":"https://example1.com"
      },
      "-"{
         "id":1123,
         "key":"_additional_wooccm2",
         "value":"https://example2.com"
      },
      "-"{
         "id":1124,
         "key":"_additional_wooccm3",
         "value":"https://example3.com"
      },
      "-"{
         "id":1131,
         "key":"_woocs_order_rate",
         "value":"0.0134310481"
      },
      "-"{
         "id":1132,
         "key":"_woocs_order_base_currency",
         "value":"INR"
      },
      "-"{
         "id":1133,
         "key":"_woocs_order_currency_changed_mannualy",
         "value":"0"
      }
   ],
   "-""line_items":[
      "-"{
         "id":8,
         "name":"License Key for 1 Year. [3 Websites]",
         "product_id":79,
         "variation_id":0,
         "quantity":1,
         "tax_class":"",
         "subtotal":"19.95",
         "subtotal_tax":"0.00",
         "total":"0.00",
         "total_tax":"0.00",
         "-""taxes":[
            
         ],
         "-""meta_data":[
            
         ],
         "sku":"",
         "price":0,
         "parent_name":null
      }
   ],
   "-""tax_lines":[
      
   ],
   "-""shipping_lines":[
      
   ],
   "-""fee_lines":[
      
   ],
   "-""coupon_lines":[
      "-"{
         "id":9,
         "code":"mj53d8ns",
         "discount":"19.95",
         "discount_tax":"0",
         "-""meta_data":[
            "-"{
               "id":63,
               "key":"coupon_data",
               "-""value":{
                  "id":133,
                  "code":"mj53d8ns",
                  "amount":"1500",
                  "-""date_created":{
                     "date":"2020-11-17 19:11:38.000000",
                     "timezone_type":1,
                     "timezone":"+00:00"
                  },
                  "-""date_modified":{
                     "date":"2020-11-17 19:11:38.000000",
                     "timezone_type":1,
                     "timezone":"+00:00"
                  },
                  "date_expires":null,
                  "discount_type":"fixed_cart",
                  "description":"",
                  "usage_count":2,
                  "individual_use":false,
                  "-""product_ids":[
                     
                  ],
                  "-""excluded_product_ids":[
                     
                  ],
                  "usage_limit":0,
                  "usage_limit_per_user":0,
                  "limit_usage_to_x_items":null,
                  "free_shipping":false,
                  "-""product_categories":[
                     
                  ],
                  "-""excluded_product_categories":[
                     
                  ],
                  "exclude_sale_items":false,
                  "minimum_amount":"",
                  "maximum_amount":"",
                  "-""email_restrictions":[
                     
                  ],
                  "virtual":false,
                  "-""meta_data":[
                     
                  ]
               },
               "display_key":"coupon_data",
               "-""display_value":{
                  "id":133,
                  "code":"mj53d8ns",
                  "amount":"1500",
                  "-""date_created":{
                     "date":"2020-11-17 19:11:38.000000",
                     "timezone_type":1,
                     "timezone":"+00:00"
                  },
                  "-""date_modified":{
                     "date":"2020-11-17 19:11:38.000000",
                     "timezone_type":1,
                     "timezone":"+00:00"
                  },
                  "date_expires":null,
                  "discount_type":"fixed_cart",
                  "description":"",
                  "usage_count":2,
                  "individual_use":false,
                  "-""product_ids":[
                     
                  ],
                  "-""excluded_product_ids":[
                     
                  ],
                  "usage_limit":0,
                  "usage_limit_per_user":0,
                  "limit_usage_to_x_items":null,
                  "free_shipping":false,
                  "-""product_categories":[
                     
                  ],
                  "-""excluded_product_categories":[
                     
                  ],
                  "exclude_sale_items":false,
                  "minimum_amount":"",
                  "maximum_amount":"",
                  "-""email_restrictions":[
                     
                  ],
                  "virtual":false,
                  "-""meta_data":[
                     
                  ]
               }
            }
         ]
      }
   ],
   "-""refunds":[
      
   ],
   "currency_symbol":"₹",
   "-""_links":{
      "-""self":[
         "-"{
            "href":"https://example.com/wp-json/wc/v3/orders/136"
         }
      ],
      "-""collection":[
         "-"{
            "href":"https://example.com/wp-json/wc/v3/orders"
         }
      ],
      "-""customer":[
         "-"{
            "href":"https://example.com/wp-json/wc/v3/customers/2"
         }
      ]
   }
}
]

Hey @mcnaveen!

Do you want all the values that are in the meta_data object or do you want the values that the keys _additional_wooccm1, _additional_wooccm2 and _additional_wooccm3 contains?

If you want the latter, below is the code snippet that might help.

let index = 0;
for (const data of $node["WooCommerce Trigger"].json["meta_data"]) {
  if(data.key.match(/_additional_wooccm/)){
    items[0].json[`url${index++}`] = data.value.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0];
  }
}
return items;

We are using a regular expression to check if the key contains _additional_woocm or not. If it does, we are adding its value to the items array.

Hope this helps. :slightly_smiling_face:

Thanks @harshil1712 It works.

I am glad I could help you. :slightly_smiling_face:

Have fun!

1 Like