Creating custom web hook topic or function for custom payload for WooCommerce

2.2k views Asked by At

I am new to working with webhooks and APIs, so am a little out of my depth and really need some help with creating a custom webhook, with a custom payload. Have read lots and tried different functions to see if I can get my head around it, but they are not giving me the result I want and to be honest, now confusing me. Can someone please explain, how to create a webhook topic or function, with a custom payload for order.complete

The result I am trying to achieve is to send a small amount of data, in the following array

‘REF’ = Order key,
‘NAME’ = Users full name,
‘EMAIL’ = Billing Email Address,
‘PROD’ = prodcodeID, (custom product field setup)
‘VER’ = versionID, (custom product field setup)

I also need to add in a date with 90 days added to it.

‘DATE’ = completed_at +90 days in the following format Y-m-d

Any help or guidance would be greatly appreciated.


So far I have created the custom fields and they are displayed properly on the product admin page and storing the data, using the following

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

  global $woocommerce, $post;

  echo '<div class="options_group">';

  // Custom fields will be created here...

  // Number Field
woocommerce_wp_text_input( 
    array( 
        'id'                => 'versionID', 
        'label'             => __( 'Version', 'woocommerce' ), 
        'placeholder'       => '', 
        'description'       => __( 'Enter the custom value here.', 'woocommerce' ),
        'type'              => 'number', 
        'custom_attributes' => array(
                'step'  => 'any',
                'min'   => '0'
            ) 
    )
);

  // Number Field
woocommerce_wp_text_input( 
    array( 
        'id'                => 'productCodeID', 
        'label'             => __( 'Product code', 'woocommerce' ), 
        'placeholder'       => '', 
        'description'       => __( 'Enter the custom value here.', 'woocommerce' ),
        'type'              => 'number', 
        'custom_attributes' => array(
                'step'  => 'any',
                'min'   => '0'
            ) 
    )
);
  echo '</div>';

}


function woo_add_custom_general_fields_save( $post_id ){
  // Number Field
    $woocommerce_number_field = $_POST['_version_ID'];
    if( !empty( $woocommerce_number_field ) )
        update_post_meta( $post_id, '_version_ID', esc_attr( $woocommerce_number_field ) );

  // Number Field
    $woocommerce_number_field = $_POST['_product_Code_ID'];
    if( !empty( $woocommerce_number_field ) )
        update_post_meta( $post_id, '_product_Code_ID', esc_attr( $woocommerce_number_field ) );
}

When trying to call one of the fields, with the below, I get it show up in the standard JSON output of the woocommerce order.created webhook, but it appears with no value

function my_custom_orders_api_fields( $order_data, $order ) {
    $order_data['VER'] = get_post_meta( $order->id, '_version_ID', true );

    return $order_data;
}

add_filter( 'woocommerce_api_order_response', 'my_custom_orders_api_fields', 10, 2 );

JSON Output

 "VER":"",

Any assistance would be greatly appreciated.

0

There are 0 answers