Search code examples
wordpresswoocommercewebhookszapier

WP Webhooks Pro with WooCommerce extension not sending line items


Is there any way to make this happen? Using this via "Send data on new post" but it isn't including the line items in the payload. Trying to send it to a Zapier Catch Raw Hook zap.


Solution

  • For sending data from Woocommerce using webhooks, you can use the webhooks functionality that is integrated into Woocommerce. More details on that are available here: https://docs.woocommerce.com/document/webhooks/

    If you wish to do that using WP Webhooks or WP Webhooks Pro, you can include the following WordPress hook into the functions.php file of your theme:

    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;
    
    }
    

    This extends the response of the create_post calls and adds the line items of your order to it. You can customize the values that you would like to send based on your needs - simply add them within the single line item within the code above. In case you choose to send the data using the JSON format, it will add something like this to your request:

    "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": ""
        }
    

    In case you want to use this more professionally, you can also create an own extension for WP Webhooks and/or WP Webhooks Pro. There's a plugin template available that lets you add a webhook fairly simple: Visit their documentation on it