I am exporting WooCommerce orders to a google sheets using WooCommerce Webhooks and Apps Script
I have two queires
1) How do I reference WooCommerce "add-ons" data
2) Parent order ID is not showing in google sheet
function doPost(e) {
var myData = JSON.parse([e.postData.contents]);
var timestamp = new Date();
var order_number = myData.number;
var parent_id= myData.parent_id;
var order_status = myData.status;
var billing_first_name = myData.billing.first_name;
var billing_last_name = myData.billing.last_name;
var billing_phone = myData.billing.phone;
var billing_email = myData.billing.email;
var order_total = myData.total;
var billing_address_1 = myData.billing.address_1;
var billing_address_2 = myData.billing.address_2;
var billing_city = myData.billing.city;
var billing_address = billing_address_1 + ", " + billing_address_2 + ", " + billing_city;
var billing_postcode = myData.billing.postcode;
var shipping_first_name = myData.shipping.first_name;
var shipping_last_name = myData.shipping.last_name;
var shipping_address_1 = myData.shipping.address_1;
var shipping_address_2 = myData.shipping.address_2;
var shipping_city = myData.shipping.city;
var shipping_address = shipping_address_1 + ", " + shipping_address_2 + ", " + shipping_city;
var shipping_postcode = myData.shipping.postcode;
var lineitems=""
for (i in myData.line_items)
{
var product_name = myData.line_items[i].name;
var itemName = myData.line_items[i].name;
var quantity = myData.line_items[i].quantity;
var linetotal = myData.line_items[i].total;
var product_items = quantity + " x " + itemName + ": £"+linetotal +"\n";
var lineitems =lineitems+product_items;
}
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([timestamp,order_number,parent_id,order_status,billing_first_name,billing_last_name,billing_phone,billing_email,lineitems,order_total,billing_address,billing_postcode,shipping_first_name,shipping_last_name,shipping_address,shipping_postcode]);
}
What is missing from your question is:
So we can only make a generic answer.
Also note that asking multiple questions at once is not allowed in StackOverFlow .
1) The WooCommerce add-ons data within an WooCommerce order is stored as order item custom meta data:
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
// Get the special meta data in an array:
$meta_data = $item->get_meta_data();
echo '<pre>'; print_r($meta_data); echo '<pre>'; // Testing raw output
// Get all additional meta data (formatted in an unprotected array)
$formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
echo '<pre>'; print_r($formatted_meta_data); echo '<pre>'; // Testing raw output
// Get the specific meta data from a meta_key:
$meta_value = $item->get_meta( 'custom_meta_key' );
}
Related: Get Order items and WC_Order_Item_Product in WooCommerce 3
2) To get the parent order number from a subscription you will use:
From the subscription ID we can get the order ID very easily:
$order_id = wp_get_post_parent_id( $subscription_id );
From a WC_Subscription
Object we can also get the order ID very easily:
$order_id = $subscription->get_parent_id();
Then from this order ID you can get the order number with:
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$order_mumber = $order->get_order_number();
or with:
$order_mumber = get_post_meta( $order_id, _order_number, true );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.