Для отправки данных из Woocommerce с помощью webhooks вы можете использовать функцию webhooks, интегрированную в Woocommerce. Более подробная информация об этом доступна здесь: https://docs.woocommerce.com/document/webhooks/
Если вы сделаете это sh, используя WP Webhooks или WP Webhooks Pro , вы можете включить в функции следующий хук WordPress. php файл вашей темы:
add_filter( 'wpwhpro/admin/webhooks/webhook_data', 'wpwhpro_append_woocommerce_items_to_create_post', 10, 4 );
function wpwhpro_append_woocommerce_items_to_create_post( $data, $response, $webhook, $args ){
//Make sure we only append the Woocommerce Data to the post_create webhook
if( ! isset( $webhook['webhook_name'] ) || $webhook['webhook_name'] !== 'post_create' ){
return $data;
}
//Verfiy the post id is set before adding the data
if( ! isset( $data['post_id'] ) || empty( $data['post_id'] ) ){
return $data;
}
//Won't work if Woocommerce is deactivated or not available
if( ! function_exists( 'wc_get_order' ) ){
return $data;
}
$order_id = intval( $data['post_id'] );
$order = wc_get_order( $order_id );
//Make sure there wasn't a problem with fetching the order
if( empty( $order ) || is_wp_error( $order ) ){
return $data;
}
//The array with order item data we append to the request body
$data['order_items'] = array();
$order_items = $order->get_items();
foreach( $order_items as $key => $item ){
//This is the data per product we are going to append
$single_item_data = array(
'name' => $item->get_name(),
'item_id' => $item->get_id(),
'product_id' => $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id(),
'quantity' => $item->get_quantity(),
'subtotal' => $item->get_subtotal(),
'subtotal_tax' => $item->get_subtotal_tax(),
'subtotal_tax' => $item->get_total_tax(),
'tax_class' => $item->get_tax_class(),
);
$data['order_items'][ $key ] = $single_item_data;
}
return $data;
}
Это расширяет ответ на вызовы create_post и добавляет элементы строки Ваш заказ на это. Вы можете настроить значения, которые вы хотите отправить, исходя из ваших потребностей - просто добавьте их в одну строку в приведенном выше коде. Если вы решите отправить данные в формате JSON, это добавит к вашему запросу что-то вроде этого:
"order_items": {
"36": {
"name": "Extension product",
"item_id": 36,
"product_id": 156,
"quantity": 2,
"subtotal": "4",
"subtotal_tax": "0",
"tax_class": ""
},
"37": {
"name": "Main Product",
"item_id": 37,
"product_id": 155,
"quantity": 1,
"subtotal": "5",
"subtotal_tax": "0",
"tax_class": ""
}
Если вы хотите использовать это более профессионально, вы также можете создать свой собственный расширение для WP Webhooks и / или WP Webhooks Pro. Доступен шаблон плагина, который позволяет довольно просто добавить веб-крючок: Посетите их документацию