Wordpress 带有WooCommerce扩展的WP Webhooks Pro不发送行项目

Wordpress 带有WooCommerce扩展的WP Webhooks Pro不发送行项目,wordpress,woocommerce,webhooks,zapier,Wordpress,Woocommerce,Webhooks,Zapier,有什么办法可以做到这一点吗?通过“在新帖子上发送数据”使用此选项,但它不包括有效负载中的行项目。尝试将其发送到Zapier捕获原始钩子zap。要使用webhooks从Woocommerce发送数据,可以使用集成到Woocommerce中的webhooks功能。有关这方面的更多详细信息,请访问: 如果您希望使用WP-Webhooks或WP-Webhooks-Pro执行此操作,您可以将以下WordPress-hook包含到主题的functions.php文件中: add_filter( 'wpwhp

有什么办法可以做到这一点吗?通过“在新帖子上发送数据”使用此选项,但它不包括有效负载中的行项目。尝试将其发送到Zapier捕获原始钩子zap。

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

如果您希望使用WP-WebhooksWP-Webhooks-Pro执行此操作,您可以将以下WordPress-hook包含到主题的functions.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:

谢谢!最终使用了默认的WooCommerce Webhooks,并可能最终将其切换到WP Webhooks,只是为了稍微清理一下负载。谢谢你的详尽回答!