WP Webhooks Pro与WooCommerce扩展不发送行项目



有什么办法可以做到这一点吗?通过"在新帖子上发送数据"使用它,但它不包括有效负载中的行项目。试图将其发送到Zapier Catch Raw Hook zap。

要使用webhooks从Woocommerce发送数据,您可以使用集成到Woocommerce中的webhooks功能。有关这方面的更多详细信息,请访问:https://docs.woocommerce.com/document/webhooks/

如果您希望使用WP Webhooks或WP WebhooksPro执行此操作,则可以将以下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 创建自己的扩展。有一个插件模板可用,可让您相当简单地添加 webhook:访问他们的文档

最新更新