5

I have webhook with topic Order Updated. Webhook delivers payload (Order information) at someotherdomain.com. I want to add extra field from checkout form to be delivered at someotherdomain.com along with order information.

I have created custom checkout field by:

add_action( 'woocommerce_after_order_notes', 'fs_custom_checkout_field' );


function fs_custom_checkout_field( $checkout ) {


echo '<div id="my_custom_checkout_field"><h2>' . __('Extra Information') . '</h2>';

woocommerce_form_field( 'fs_psid_field', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Fill in this field'),
    'placeholder'   => __('Enter something'),
    ), $checkout->get_value( 'fs_psid_field' ));

echo '</div>';

}

And saved meta as:

add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );


function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['fs_psid_field'] ) ) {
        update_post_meta( $order_id, 'fs_psid_field', sanitize_text_field( $_POST['fs_psid_field'] ) );
    }
}

But field fs_psid_field is not posted via webhook.

Q: How can I post value of this field via Woocommerce webhook?

Saqib Omer
  • 5,387
  • 7
  • 50
  • 71

1 Answers1

12

Figured out. First add custom field on check out form.

add_action( 'woocommerce_after_order_notes', 'fs_custom_checkout_field' );

function fs_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h2>' . __('Extra Information') . '</h2>';

    woocommerce_form_field( 'fs_psid_field', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Fill in this field'),
        'placeholder'   => __('Enter something'),
        ), $checkout->get_value( 'fs_psid_field' ));

    echo '</div>';

}

Then save custom field's value as meta

add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['fs_psid_field'] ) ) {
        update_post_meta( $order_id, 'fs_psid_field', sanitize_text_field( $_POST['fs_psid_field'] ) );
    }
}

Then add custom field's value in api response

function my_custom_wc_api_order_response( $order_data, $order ) {

    $psidMeta      = get_post_meta($order->id , 'fs_psid_field' , true );
    $order_data['psid'] = $psidMeta;
    return $order_data;
}

add_filter( 'woocommerce_api_order_response', 'my_custom_wc_api_order_response', 10, 3 );
Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
  • This works great which is a lot further than I was. Is it possible to cancel the webhook call given certain parameters in the data? more specifically, if the order is created i want to not send a POST to my site, if the status changes to paid, then yes i do want to send the POST only with customer specific fields. sorry if my terminology is not correct . – Ryan Nov 30 '17 at 18:20
  • You would need ajax call plus web hooks in that case. – Saqib Omer Nov 30 '17 at 18:22
  • @SaqibOmer how to i know which api use in particluar hook – BInjal Patel Aug 25 '20 at 07:46
  • Depends on your requirement. Check woo commerce documentation regarding web hooks. – Saqib Omer Aug 25 '20 at 08:01