Co-Social

Connect WhatsApp to WooCommerce: The Complete Setup Guide

·5 min read woocommercewhatsappstore-apiintegration

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:

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:

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:

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?

Key endpoints:

How to integrate with WhatsApp:

  1. When a customer adds items to cart, you can store their WhatsApp number (if they provide it).
  2. Use the Store API to fetch the cart contents and generate a WhatsApp message with a checkout link.
  3. Send the message via WhatsApp Business API.

Example flow for abandoned cart:

Custom theme considerations:

Step-by-Step: Connect WhatsApp to WooCommerce with Store API

Here's a practical implementation plan:

  1. Set up WhatsApp Business API: Choose a provider and get your API key.
  2. Create a custom endpoint in WordPress: Add a REST route that accepts a cart ID and WhatsApp number.
  3. Use Store API to fetch cart: In your endpoint, call GET /wp-json/wc/store/cart?cart_id=... to get items.
  4. Generate message: Compose a message with product names, quantities, and a checkout link.
  5. Send via WhatsApp API: Use the provider's SDK to send the message.
  6. 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

Testing Your Integration

Before going live, test with a small group:

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.

اقرأ هذا المقال بالعربية

Run WhatsApp, Instagram and Messenger from one inbox, with AI replies trained on your own products. First month free.

Start free