sodcalculator

10 Tech How-To

Route WooCommerce Order Alerts to Telegram: Webhook Guide

Guide 10-33905 · Updated · Step by step

This guide shows you how to send WooCommerce order alerts to Telegram using a webhook, so you and your team get instant notifications on your phone or desktop whenever a new order, or a specific order status change, happens. You’ll need a Telegram account, a WordPress site with WooCommerce, and access to your theme’s functions.php file or a code-snippet plugin. No paid extension is required.

What You’ll Need

  • A Telegram account (free) and the Telegram app installed on your device.
  • A WordPress site with WooCommerce installed and active.
  • Admin access to your WordPress dashboard.
  • A code editor or a plugin like Code Snippets (free) to safely add custom PHP code.

Step-by-Step

  1. Create a Telegram Bot
    Open Telegram and search for BotFather (the official bot that creates bots). Start a chat and send /newbot. Follow the prompts to choose a name for your bot (e.g., “My Store Alerts”) and a username (must end in bot, like MyStoreAlertsBot). After creation, BotFather will give you an API token—a long string like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11. Copy and save it securely; you’ll need it in a moment.
  2. Get Your Chat ID
    Your bot needs to know where to send messages. The easiest way is to message your bot first. Open Telegram, search for your bot’s username, and press Start. Then, visit the following URL in your browser (replace YOUR_BOT_TOKEN with the token from step 1):
    https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates
    You’ll see a JSON response. Look for "chat":{"id":123456789}—that number is your chat ID. Copy it. If you want to send alerts to a group, add your bot to the group, send a message in the group, and then use the same URL to find the group’s chat ID (it will be a negative number).
  3. Add the Webhook Code to WordPress
    You have two safe ways to add the code: via a child theme’s functions.php or via a code snippet plugin. For the snippet plugin, install and activate Code Snippets (free). Go to Snippets > Add New. Give it a title like “Telegram Order Alerts”. Paste the following PHP code, replacing YOUR_BOT_TOKEN and YOUR_CHAT_ID with the values you saved:
    add_action( 'woocommerce_new_order', 'send_telegram_order_alert', 10, 1 );
    function send_telegram_order_alert( $order_id ) {
        $order = wc_get_order( $order_id );
        if ( ! $order ) return;
        $bot_token = 'YOUR_BOT_TOKEN';
        $chat_id = 'YOUR_CHAT_ID';
        $message = "🛒 New Order #" . $order_id . "\n";
        $message .= "Total: " . $order->get_formatted_order_total() . "\n";
        $message .= "Customer: " . $order->get_billing_first_name() . " " . $order->get_billing_last_name() . "\n";
        $message .= "Items: ";
        foreach ( $order->get_items() as $item ) {
            $message .= $item->get_name() . " x" . $item->get_quantity() . ", ";
        }
        $message = rtrim( $message, ', ' );
        $url = "https://api.telegram.org/bot" . $bot_token . "/sendMessage?chat_id=" . $chat_id . "&text=" . urlencode( $message );
        wp_remote_get( $url );
    }

    Click Save Changes and then Activate the snippet.

  4. Test the Alert
    Place a test order on your site (you can use the built-in WooCommerce sample product or create a simple product with a low price). As soon as the order is placed, you should receive a Telegram message from your bot. If you don’t see it, check the Troubleshooting section below.
  5. Customize for Other Order Statuses (Optional)
    If you want alerts for other statuses like processing, completed, or cancelled, you can hook into woocommerce_order_status_changed instead. Replace the add_action line with:
    add_action( 'woocommerce_order_status_changed', 'send_telegram_order_status_alert', 10, 4 );
    Then modify the function to accept four parameters: $order_id, $old_status, $new_status, and $order. Use the $new_status in your message, e.g., “Order #123 changed from pending to processing”.

Common Mistakes

  • Using the wrong chat ID: The chat ID is not your phone number or username. It’s a numeric ID from the getUpdates response. If you use the wrong ID, Telegram returns an error like “chat not found”.
  • Forgetting to start the bot: You must send /start to your bot before it can message you. Without that, the bot cannot initiate a conversation.
  • Hardcoding the token in a public theme: If you place the code in a theme’s functions.php and the theme is shared or public, your token could be exposed. Use a snippet plugin or keep it in a private child theme.
  • Not using urlencode: If your message contains spaces or special characters, the URL will break. Always use urlencode on the message text, as shown in the code.
  • Ignoring caching: Some caching plugins may prevent the webhook from firing immediately. Clear your site cache after adding the snippet.

Tips

  • Add a timestamp: Include the order date in your message by adding $order->get_date_created()->date_i18n( 'Y-m-d H:i:s' ) to the message.
  • Use Markdown: Telegram supports basic Markdown in messages. You can wrap the order number in * to make it bold, but remember to set parse_mode=Markdown in the URL.
  • Send to a group: Instead of a personal chat, create a Telegram group for your team, add the bot, and use the group’s chat ID. This way everyone gets the alert.
  • Handle errors gracefully: Wrap the wp_remote_get call in a check to avoid slowing down checkout. For example, use wp_remote_post with a timeout of 5 seconds and ignore the response.
  • Log the response: For debugging, you can log the API response to the WooCommerce logs using error_log or a logger class.

Troubleshooting

  • No message received: First, verify your bot token and chat ID are correct. Test the API URL directly in your browser: https://api.telegram.org/botYOUR_BOT_TOKEN/sendMessage?chat_id=YOUR_CHAT_ID&text=Hello. If that works, the issue is in your WordPress code—check for syntax errors or that the hook is firing. Try adding a temporary error_log line to confirm the function runs.
  • Message says “chat not found”: This means the chat ID is wrong. Double-check the getUpdates response. Ensure you messaged your bot first. If using a group, make sure the bot is a member of the group.
  • Order placed but no alert: WooCommerce may be using AJAX for checkout, which can interfere with hooks. Try placing an order via the block-based checkout or check if a plugin like a page builder is blocking the hook. Also, clear any caching plugins.
  • Alert appears twice: You might have the snippet active twice (e.g., in both a plugin and your theme). Disable one copy.
  • Site slows down after adding code: The wp_remote_get call is synchronous and can delay the thank-you page. To avoid this, use wp_remote_post with a short timeout, or schedule a cron event to send the message asynchronously.

Frequently Asked Questions

Can I send alerts for specific products only?

Yes. In the function, loop through the order items and check if the product ID matches your target. If it doesn’t, return early and skip the notification.

Will this work with WooCommerce subscriptions or other plugins?

Yes, the woocommerce_new_order hook fires for any order, including subscription renewals. For subscription-specific events, you may need to hook into WooCommerce Subscriptions’ own hooks, but the same Telegram API method applies.

Is there a way to send alerts without coding?

Yes, there are paid extensions like “Telegram for WooCommerce” that provide a user-friendly setup. However, this webhook method is free and works reliably, and the code is simple enough to copy and paste.

Can I receive alerts on multiple devices?

Yes, Telegram syncs across all your devices. As long as you’re logged into the same account, you’ll get the alert on your phone, tablet, and desktop.

What if my chat ID is wrong?

You’ll see an error like “Bad Request: chat not found” in the API response. To fix it, repeat the getUpdates step, ensuring you’ve messaged your bot first. If you’re using a group, make sure the bot is added and you’ve sent a message in the group.

Can I customize the message format?

Absolutely. The message is built as a string in PHP. You can add or remove lines, include custom fields, or format it with emojis and line breaks. Just be careful to escape quotes and use \n for new lines.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →