Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter tommyller

    (@tommyller)

    Thank you @bcworkz , this resolved my issue! Here is the final code, for adding a costum response in woocommerce api that retrives data from a costum field for a order, where _team_name_field is the custom field used for this example:

    add_filter( 'woocommerce_rest_prepare_shop_order_object', 'my_wc_prepare_shop_order', 10, 3 );
    
    function my_wc_prepare_shop_order( $response, $object, $request ) {
    $order_data = $response->get_data();
    
    foreach ( $order_data['line_items'] as $key => $item ) {
    $order_data['line_items'][ $key ]['Team_name'] = get_post_meta( $object->get_id(), '_team_name_field', true ) ;
    }
    		
    $response->data = $order_data;
    return $response;
    }
    • This reply was modified 3 years, 8 months ago by tommyller.
    Thread Starter tommyller

    (@tommyller)

    Yes, it is the orders ID im after. I think it is out of scope too, but I can’t figur out how to get the data. Im using this function to save data from the fields, if it helps:

    function vicodemedia_save_extra_checkout_fields( $order_id, $posted ){
        if( isset( $posted['team_name_field'] ) ) {
            update_post_meta( $order_id, '_team_name_field', sanitize_text_field( $posted['team_name_field'] ) );
        }
    }
    add_action( 'woocommerce_checkout_update_order_meta', 'vicodemedia_save_extra_checkout_fields', 10, 2 );
Viewing 2 replies - 1 through 2 (of 2 total)