$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] ] ] ] ] ]
Step One: Get your module ready
- /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:
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
$ php bin/magento setup:upgrade
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.