How to Integrate WooCommerce with Slack to receive purchase notifications

If you want to receive Slack notifications whenever a purchase is made in your WooCommerce store, you can easily achieve this using the woocommerce_payment_complete hook. In this article, I will show you how to integrate WooCommerce with Slack so you receive alerts in a specific channel when a payment is completed.

 

Prerequisites

  1. A functioning WooCommerce store.
  2. A Slack workspace.
  3. A Slack authentication token to use the modern API.
  4. The Slack channel ID where messages will be sent.

 

Create an API Token in Slack

To send messages to Slack, we need to generate an access token:

  1. Go to Slack API and create a new application.
  2. In the OAuth & Permissions settings, add the chat:write permission (this allows using chat.postMessage).
  3. Authorize the application in your workspace.
  4. Copy the generated authentication token; we will need it in the code.

 

Get the Slack channel ID

To send messages to a Slack channel, you need its ID. Follow these steps to obtain it:

  1. Open Slack in the browser or desktop application.
  2. Go to the channel where you want to receive notifications.
  3. Click on the channel name at the top.
  4. Scroll down to the Channel Info section and copy the Channel ID (it will look something like C0123456789).

 

Add the code to WooCommerce

Add the following code to your theme’s  functions.php file or a custom plugin:

<?php
function send_slack_notification( $order_id ) {
    $slack_token = 'xoxb-YOUR-ACCESS-TOKEN'; // Replace with your Slack token
    $channel_id = 'C0123456789'; // Replace with your Slack channel ID
    
    $order = wc_get_order( $order_id );
    
    $message = "📢 *New Purchase Made*\n";
    $message .= "🛒 Order #$order_id\n";
    $message .= "👤 Customer: " . $order->get_billing_first_name() . " " . $order->get_billing_last_name() . "\n";
    $message .= "💰 Total: " . wc_price( $order->get_total() ) . "\n";
    
    $payload = json_encode([
        "channel" => $channel_id,
        "text" => $message
    ]);
    
    $args = [
        'body'    => $payload,
        'headers' => [
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . $slack_token
        ],
        'timeout' => 10
    ];
    
    wp_remote_post( 'https://slack.com/api/chat.postMessage', $args );
}

add_action( 'woocommerce_payment_complete', 'send_slack_notification' );

 

Code explanation

  1. We get the authentication token generated in Slack.
  2. We get the channel ID where notifications will be sent.
  3. We retrieve the order using wc_get_order( $order_id ).
  4. We construct the message with the purchase details.
  5. We send the notification to Slack using the modern  chat.postMessage API.
  6. We connect the function to the woocommerce_payment_complete hook.

 

Test the integration

  1. Make a test purchase in your WooCommerce store.
  2. Verify that the message arrives correctly in the configured Slack channel.
  3. If you don’t see the notification, check the WordPress error log using error_log().

 

This integration is an effective way to receive real-time alerts about new sales in your WooCommerce store. If you want to further customize the notification, you can include additional information such as purchased products or payment method.

Try this solution and stay informed about your store’s sales without leaving Slack!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to Top
0
Would love your thoughts, please comment.x
()
x