• Resolved timosoru

    (@timosoru)


    Hi, I try to integrate new orders to google sheet. I find no problem to woocommerce default checkout field and the data transfered successfully to my spreadsheet.

    The problem is I couldn’t find any data from additional checkout field.

    This is my script:

    //this is a function that fires when the webapp receives a GET request
    function doGet(e) {
    return HtmlService.createHtmlOutput(“request received”);
    }

    //this is a function that fires when the webapp receives a POST request
    function doPost(e) {
    var myData = JSON.parse([e.postData.contents]);
    var order_number = myData.number;
    var order_created = myData.date_created;
    var billing_first_name = myData.billing.first_name;
    var billing_phone = myData.billing.phone;
    var shipping_first_name= myData.shipping.first_name;
    var shipping_hp = myData.wcf.shipping_hp;
    var shipping_address_1 = myData.shipping.address_1;
    var shipping_address_2 = myData.shipping.address_2;
    var shipping_city = myData.shipping.city;
    var shipping_state = myData.shipping.state;
    var product_name = myData.line_items[0].name;
    var sku = myData.line_items[0].sku;
    var product_qty = myData.line_items[0].quantity;
    var payment_method = myData.payment_method_title;
    var shipping_kurir = myData.wcf.shipping_kurir;
    var shipping_ongkir = myData.wcf.shipping_ongkir;
    var shipping_jual = myData.wcf.shipping_jual;
    var order_comments = myData.wcf.order_comments;

    var sheet = SpreadsheetApp.getActiveSheet();
    for (var i = 0; i < myData.line_items.length; i++)
    { var product_name = myData.line_items[i].name;
    var sku = myData.line_items[i].sku;
    var product_qty = myData.line_items[i].quantity;
    sheet.appendRow([order_number,order_created,billing_first_name,billing_phone,shipping_first_name,shipping_hp,shipping_address_1,shipping_address_2,shipping_city,shipping_state,product_name,sku,product_qty,payment_method,shipping_kurir,shipping_ongkir,shipping_jual,order_comments]);}
    }

    I tried meta key order_metadata and still not working. I have no programming skills just try to find solution for this.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author ThemeHigh

    (@themehigh)

    We hope you are checking the custom data in the API list for the WooCommerce. If so, unfortunately, API is not supported in the lite version of our plugin.

    Thank you!

    Thread Starter timosoru

    (@timosoru)

    Thank you for your answer. But I still don’t get it since I’m not a programmer and can’t get it.

    I know that your data form saved as order meta data. I can see that when I debug the webhook body data after create an order. Also, I can see the custom data in the API list. My question is how could I transfer it to my Google App Script?

    This is the order meta data properties from API list WooCommerce:
    Attribute Type Description
    id integer Meta ID. READ-ONLY
    key string Meta key.
    value string Meta value.

    Then, I try this code (for example): var shipping_hp = myData.meta_data.key_shipping_hp;

    But still get no results. It still empty. I really need any solution about this.

    Thank you for your responses!

    Plugin Author ThemeHigh

    (@themehigh)

    We believe that you are using the latest version of WooCommerce REST API.

    You can easily add custom meta value in the WooCommerce REST API response. Just add below code snippet in your child theme’s functions.php file. You need to modify the code as per your field name or meta key.

    function th43er_wc_rest_prepare_order_object( $response, $object, $request ) {
    	// Get the meta value
    	$your_field_value = ( $value = get_post_meta($object->get_id(), 'your_field_name', true) ) ? $value : '';
    	
    	// Set the meta value in API responce
    	$response->data['your_field_name'] = $your_field_value;
    
    	return $response;
    }
    add_filter( 'woocommerce_rest_prepare_shop_order_object', 'th43er_wc_rest_prepare_order_object', 10, 3 );

    Now in Google Apps script, you can collect the value like below one.

    var your_field_value = myData.your_field_name;

    We hope this will help.

    Thank you!

    Thread Starter timosoru

    (@timosoru)

    Thanks for your response.

    I’ve figured out how to write meta data value in Google Apps script.

    I wrote this:
    var your_meta_data_field_key = myData.meta_data[i]["value"];

    i = the value based on your webhook debug. should be started with 0.

    And it worked!

    Thank you, ThemeHigh team.

    Plugin Author ThemeHigh

    (@themehigh)

    Great!

    Hi, i have this situation:

    In function.php:

    
    function th43er_wc_rest_prepare_order_object( $response, $object, $request ) {
    	// Get the meta value
    	$your_field_value = ( $value = get_post_meta($object->get_id(), 'wflink', true) ) ? $value : '';
    	
    	// Set the meta value in API responce
    	$response->data['wflink'] = $your_field_value;
    
    	return $response;
    }
    add_filter( 'woocommerce_rest_prepare_shop_order_object', 'th43er_wc_rest_prepare_order_object', 10, 3 );
    
    

    IN GOOGLE SHEET SCRIPT:

    var your_field_value = myData.wflink;

    I have put a custom field in a product called wflink but in google sheet following the code on top in this discussion i receive all other order information but my custom field column is still empty..

    Why?

    I have try also:

    
    var your_meta_data_field_key = myData.meta_data[i]["wflink"];
    

    but not work.

    Can you help me? Thank you!

    • This reply was modified 4 years, 3 months ago by cronlin.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Meta Key for Google App Script’ is closed to new replies.