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

问题描述 投票:0回答:1

有什么办法可以做到这一点?通过“在新帖子上发送数据”使用此功能,但有效负载中不包括订单项。尝试将其发送到Zapier Catch Raw Hook zap。

wordpress woocommerce webhooks zapier
1个回答
0
投票

要使用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和/或WP Webhooks Pro创建自己的扩展名。有一个可用的插件模板,使您可以相当简单地添加一个Webhook:Visit their documentation on it

© www.soinside.com 2019 - 2024. All rights reserved.