How to Send WhatsApp Notifications for WooCommerce Orders?

There is no official plugin yet for WooCommerce, but there is an easy and straightforward hack to send an the order notification of WooCommerce through WhatsApp.

Read more: How to Send WhatsApp Notifications for WooCommerce Orders?

For this solution you need an Pro or Elite subscription for Send.icu

To add the WooCommerce order notification plugin to send WhatsApp messages, follow these steps:

1. Plugin Setup

Create a new folder for your plugin and include the necessary files.

  1. Create Plugin Folder: Create a folder named woocommerce-whatsapp-notifications in the wp-content/plugins/ directory.
  2. Create Main Plugin File: Inside the folder, create a file named woocommerce-whatsapp-notifications.php.

2. Main Plugin File

Add the following code to the woocommerce-whatsapp-notifications.php file:

phpCopy codeget_billing_phone() . '@s.whatsapp.net';
    $msg = get_whatsapp_order_message($order, $new_status);
    
    $response = send_whatsapp_message($jid, $msg);
    save_whatsapp_message_log($order_id, $msg, $response);

    if (!$response['success']) {
        // Handle error
        error_log('Failed to send WhatsApp notification: ' . $response['message']);
    }
}

function send_whatsapp_message($jid, $msg) {
    $token = get_option('whatsapp_api_token');
    $instance_id = get_option('whatsapp_api_instance_id');

    $response = wp_remote_get("https://app.send.icu/api/v1/send-text?token=$token&instance_id=$instance_id&jid=$jid&msg=" . urlencode($msg));
    if (is_wp_error($response)) {
        return array('success' => false, 'message' => $response->get_error_message());
    }
    
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);
    
    return $data;
}

function get_whatsapp_order_message($order, $new_status) {
    return "Hello " . $order->get_billing_first_name() . ", your order #" . $order->get_id() . " status has changed to " . $new_status . ".";
}

function save_whatsapp_message_log($order_id, $msg, $response) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'whatsapp_message_logs';
    $wpdb->insert($table_name, array(
        'order_id' => $order_id,
        'message' => $msg,
        'response' => maybe_serialize($response),
        'sent_at' => current_time('mysql')
    ));
}

// Create logs table on plugin activation
function whatsapp_create_logs_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'whatsapp_message_logs';
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        order_id mediumint(9) NOT NULL,
        message text NOT NULL,
        response text NOT NULL,
        sent_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}
register_activation_hook(__FILE__, 'whatsapp_create_logs_table');

3. Settings Page

Create the class-wc-settings-whatsapp.php file inside an includes folder:

phpCopy codeid    = 'whatsapp';
        $this->label = __('WhatsApp API', 'woocommerce');
        parent::__construct();
    }

    public function get_settings() {
        return array(
            'section_title' => array(
                'name'     => __('WhatsApp API Settings', 'woocommerce'),
                'type'     => 'title',
                'desc'     => '',
                'id'       => 'whatsapp_api_settings',
            ),
            'token' => array(
                'name' => __('API Token', 'woocommerce'),
                'type' => 'text',
                'desc' => __('Enter your WhatsApp API token.', 'woocommerce'),
                'id'   => 'whatsapp_api_token',
            ),
            'instance_id' => array(
                'name' => __('Instance ID', 'woocommerce'),
                'type' => 'text',
                'desc' => __('Enter your WhatsApp API instance ID.', 'woocommerce'),
                'id'   => 'whatsapp_api_instance_id',
            ),
            'section_end' => array(
                'type' => 'sectionend',
                'id'   => 'whatsapp_api_settings',
            ),
        );
    }
}

return new WC_Settings_WhatsApp();

4. Activate the Plugin

  1. Upload the Plugin: Upload the woocommerce-whatsapp-notifications folder to the wp-content/plugins/ directory on your server.
  2. Activate the Plugin: Go to the WordPress admin dashboard, navigate to “Plugins,” find “WooCommerce WhatsApp Notifications,” and click “Activate.”
  3. Configure Settings: Go to WooCommerce settings, find the new “WhatsApp API” tab, and enter your WhatsApp API token and instance ID.

This setup will enable WhatsApp notifications for WooCommerce order status changes. When an order status changes, a WhatsApp message will be sent to the customer’s phone number with the updated status.

Michiel is the visionary co-founder of Send.icu, a cutting-edge platform revolutionizing WhatsApp marketing. With a background in marketing and tech and a passion for leveraging technology to drive business growth, Michiel has spearheaded innovative solutions that blend strategic insight with tech-savvy execution. His expertise in analytics and digital communication has been pivotal in shaping Send.icu into a leading tool for optimizing marketing ROI and enhancing customer engagement. Michiel's commitment to excellence and forward-thinking approach continues to drive Send.icu's mission of transforming digital marketing strategies for businesses worldwide.

Leave a Reply

Your email address will not be published. Required fields are marked *