WhatsApp Business API | Magento Integration
Hi there!
Would you like to send WhatsApp confirmation to your customers automatically when they place an order?
In this tutorial, you will learn to integrate the Magento eCommerce platform with WhatsApp Business using the tyntec Conversations API. This integration allows you to send automated messages to your customers when they place an order in a Magento store.
You will need
-
- Your WABA phone number that has been approved by tyntec
-
- Your tyntec API key from your tyntec account
-
- A phone with WhatsApp as a testing customer
-
- Magento 2 installation (Magento Open Source is enough for a demo)
Step One: Get your module ready
For your quick start with integrations, we’ve prepared a sample module. Let’s look at what the code does. You can notice that the code is in several files:
- /registration.php allows registering this sample app as a Magento module.
- In the /etc folder:
- module.xml provides Magento with information about the module.
- events.xml tells Magento which event triggers the code.
- In the /Observer folder, you find the file Orderplaceafter.php. This code is responsible for sending a WhatsApp message after a customer places an Order in your Magento store. See also Magento Events and observers.
Let’s get the code ready:
1. Clone the api-samples repository.
2. Copy the folder api-samples/wa-integrations/magento/Tyntec to <MAGENTO_HOME>/app/code/Tyntec.
3. Adapt the message template (and parameters, eventually) in the JSON body in the Orderplaceafter.php observer. The JSON body looks like this in PHP:
$options['json'] = [
'to' => $phone,
'from' => $_ENV['WABA_NUMBER'],
'channel' => 'whatsapp',
'content' => [
'contentType' => 'template',
'template' => [
'templateId' => '<YOUR_TEMPLATE_ID>',
'templateLanguage' => 'en',
'components' => [
['body'=> [
['type'=>'text', 'text'=>$orderNumber]
]
]
]
]
]
You may also retrieve additional data from the order event for your message.
4. Copy Observer/.env-example to Observer/.env and set environment variables in .env:
- WABA_NUMBER — your WABA number in the international format
- TYNTEC_API_KEY — your API key from tyntec to authenticate calls to the Conversations API
Step Two: Install your module
Now you need to upgrade Magento to register and activate your module using the following command:
$ php bin/magento setup:upgrade
Note: Our sample module uses libraries that come with the Magento installation (guzzle and dotenv). If you wish to use other libraries, you have to add them through the composer first.
Step Three: Try it out!
It’s testing time!
Place an order from your Magento store. You can even do that directly from the admin panel for test purposes. Remember to fill in your testing phone number within the billing address.
And voila! You should have received a notification on your testing WhatsApp right away.
More?
You may want to notify your customers when their order is shipped or delivered for example. For that, you would need to add more observers and connect them to additional events.