Woocommerce – custom variable in webhooks

May 26, 2016 | Blog |

Woocommerce has the most flexible webhook system where you can include your own custom fields based on the order, products etc

The action ‘woocommerce_webhook_payload’ helps us to perform the hooking process.

For eg, Send the signup and subscription price of a product in the order using the below action.

add_action('woocommerce_webhook_payload', 'kd_woocommerce_webhook_payload');
function kd_woocommerce_webhook_payload($payload, $resource, $resource_id, $id) {
$i = 0;
if(is_array($payload['order']["line_items"])){
foreach($payload['order']["line_items"] as $items){
if($items['product_id']>0){
if(get_post_meta( $items['product_id'], '_subscription_sign_up_fee', true )){
$payload["order"]["line_items"][$i]['signup_fee'] = get_post_meta( $items['product_id'], '_subscription_sign_up_fee', true );
}
if(get_post_meta( $items['product_id'], '_subscription_price', true )){
$payload["order"]["line_items"][$i]['monthly_fee'] = get_post_meta( $items['product_id'], '_subscription_price', true );
}
}
$i++;
}
}
return $payload;
}