Connect WhatsApp to WooCommerce: The Complete Setup Guide
Connecting WhatsApp to WooCommerce means more than adding a chat button. You want to send order confirmations, share carts, and let customers ask about products directly from your store. This guide shows you how, with a focus on the WooCommerce Store API and custom themes.
Why Connect WhatsApp to WooCommerce?
Customers in Egypt, Saudi Arabia, and the Gulf often prefer WhatsApp over email. By connecting WhatsApp, you can:
- Send order confirmations and updates via WhatsApp.
- Allow customers to ask about products and get instant replies.
- Recover abandoned carts by sending a WhatsApp message with a link back to checkout.
- Provide real-time support without leaving the chat.
But to do this properly, you need to integrate WhatsApp with your WooCommerce store's data.
Methods to Connect WhatsApp to WooCommerce
There are three main approaches, each with different levels of effort and capability.
1. WhatsApp Chat Button (Basic)
This adds a floating chat button to your site. When clicked, it opens WhatsApp with a pre-filled message. It's easy but limited: you can't send order details automatically.
How to do it:
- Use a plugin like "Click to Chat" or add a custom link:
https://wa.me/1234567890?text=Hello. - For product-specific messages, you can dynamically insert the product name and URL.
Limitation: No two-way sync. You won't know who ordered what unless you check WooCommerce manually.
2. WhatsApp Business API (Advanced)
This allows programmatic sending of messages. You'll need a WhatsApp Business API provider (e.g., Twilio, MessageBird, or Co-Social). Then you can send order confirmations, shipping updates, and more.
How to do it:
- Sign up with a provider and get API credentials.
- Use WooCommerce hooks (e.g.,
woocommerce_thankyou,woocommerce_order_status_changed) to trigger WhatsApp messages. - Send messages via the provider's API.
Example code snippet (using Twilio):
```php
add_action('woocommerce_thankyou', 'send_whatsapp_order_confirmation');
function send_whatsapp_order_confirmation($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
$message = "Thank you for your order #$order_id. We'll confirm shortly.";
// Call Twilio API to send WhatsApp message
}
```
Note: This requires a server with PHP and the provider's SDK.
3. WooCommerce Store API + Custom Integration
The Store API is a REST API that exposes your store's data (products, cart, checkout) in JSON. You can use it to build a custom WhatsApp integration, especially if you have a custom theme or headless setup.
Why use the Store API?
- It's built into WooCommerce (since 3.6) and doesn't require additional plugins.
- It provides endpoints for cart manipulation, which is useful for abandoned cart recovery.
- It works well with custom themes and JavaScript frameworks.
Key endpoints:
/wp-json/wc/store/products– list products./wp-json/wc/store/cart– get or update cart./wp-json/wc/store/checkout– place order.
How to integrate with WhatsApp:
- When a customer adds items to cart, you can store their WhatsApp number (if they provide it).
- Use the Store API to fetch the cart contents and generate a WhatsApp message with a checkout link.
- Send the message via WhatsApp Business API.
Example flow for abandoned cart:
- Customer adds product to cart but doesn't check out.
- After 30 minutes, your server calls the Store API to get the cart.
- Send a WhatsApp message: "You left items in your cart. Complete your order here: [link]".
Custom theme considerations:
- If your theme uses the Store API for cart interactions, you can hook into those requests to capture the cart.
- Ensure your theme's JavaScript is compatible with the Store API's nonce and session handling.
- For headless setups, you can use the Store API from a separate frontend (e.g., React) and still send WhatsApp messages from your backend.
Step-by-Step: Connect WhatsApp to WooCommerce with Store API
Here's a practical implementation plan:
- Set up WhatsApp Business API: Choose a provider and get your API key.
- Create a custom endpoint in WordPress: Add a REST route that accepts a cart ID and WhatsApp number.
- Use Store API to fetch cart: In your endpoint, call
GET /wp-json/wc/store/cart?cart_id=...to get items. - Generate message: Compose a message with product names, quantities, and a checkout link.
- Send via WhatsApp API: Use the provider's SDK to send the message.
- Handle responses: If the customer replies, you can update the order status via WooCommerce hooks.
Code example for custom endpoint:
```php
add_action('rest_api_init', function () {
register_rest_route('myplugin/v1', '/send-cart/', array(
'methods' => 'POST',
'callback' => 'send_cart_via_whatsapp',
));
});
function send_cart_via_whatsapp($request) {
$cart_id = $request->get_param('cart_id');
$phone = $request->get_param('phone');
// Fetch cart via Store API
$response = wp_remote_get("https://yoursite.com/wp-json/wc/store/cart?cart_id=$cart_id");
$cart = json_decode(wp_remote_retrieve_body($response), true);
// Build message
$message = "Your cart: ";
foreach ($cart['items'] as $item) {
$message .= $item['name'] . " x " . $item['quantity'] . "\n";
}
$message .= "Checkout: https://yoursite.com/checkout/?cart_id=$cart_id";
// Send via WhatsApp API
// ...
return new WP_REST_Response('Message sent', 200);
}
```
Common Pitfalls and How to Avoid Them
- Phone number formatting: Always store numbers in international format (e.g., +201234567890).
- API rate limits: WhatsApp Business API has limits; don't send too many messages too quickly.
- Template messages: For business-initiated messages, you must use approved templates. Plan ahead.
- Store API authentication: The Store API uses nonces and cookies for session; when calling from server-side, you may need to use the cart token instead.
Testing Your Integration
Before going live, test with a small group:
- Place a test order and ensure the WhatsApp message is sent.
- Abandon a cart and check if the recovery message arrives.
- Reply to the WhatsApp message and see if it updates the order.
Taking It Further
If you want a ready-made solution that handles the WhatsApp Business API, order confirmations, and even AI replies trained on your products, consider Co-Social. It connects to WooCommerce and gives you one inbox for WhatsApp, Instagram, and Messenger, turning chats into confirmed orders.
But whether you build it yourself or use a tool, the key is to integrate WhatsApp deeply with your WooCommerce data. Start with the Store API, and you'll have a solid foundation.