带有 WooCommerce 扩展的 WP Webhooks Pro 不发送订单项

WP Webhooks Pro with WooCommerce extension not sending line items

有什么办法可以做到这一点吗?通过 "Send data on new post" 使用它,但它不包括有效负载中的行项目。正在尝试将其发送到 Zapier Catch Raw Hook zap。

要使用 webhooks 从 Woocommerce 发送数据,您可以使用集成到 Woocommerce 中的 webhooks 功能。此处提供更多详细信息:https://docs.woocommerce.com/document/webhooks/

如果您希望使用 WP WebhooksWP Webhooks Pro,您可以将以下 WordPress 挂钩包含到 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 创建一个自己的扩展 and/or WP Webhooks Pro。有一个可用的插件模板,可让您相当简单地添加 webhook:Visit their documentation on it