DEV Community

Murabyte
Murabyte

Posted on

Build a WhatsApp order button in PHP (and three bugs to avoid)

Disclosure: I make WaMenu at Murabyte, which is mentioned at the end.

A restaurant menu page can hand an order to WhatsApp without any API. The customer taps a button, WhatsApp opens with the order typed, and they press Send. Below is a small PHP version that builds the link on the server, plus three bugs I see often.

The server side

<?php
$number = '201001234567';           // international format, digits only
$cart = [
  ['name' => 'Koshary', 'qty' => 2],
  ['name' => 'Tea',     'qty' => 1],
];
$lines = array_map(fn($i) => $i['qty'] . ' x ' . $i['name'], $cart);
$msg = "New order\n" . implode("\n", $lines) . "\nType: pickup";
$url = 'https://wa.me/' . $number . '?text=' . rawurlencode($msg);
echo '<a href="' . htmlspecialchars($url) . '">Send order on WhatsApp</a>';
Enter fullscreen mode Exit fullscreen mode

Three bugs

  1. A plus sign or spaces in the number. wa.me wants digits only, with the country code.
  2. Using urlencode instead of rawurlencode. urlencode turns spaces into +, and WhatsApp can show the plus signs. rawurlencode gives %20.
  3. Forgetting escaping in the page. Escape the URL with htmlspecialchars when you print it in HTML, because item names can come from a database.

What the link cannot tell you

Your server does not learn whether the customer pressed Send. Treat the saved order as a draft, and let staff confirm in WhatsApp. Do not count drafts as sales.

Server-side checks

Never trust prices from the browser. Rebuild the cart on the server from your own item list, so a customer cannot edit a price in the page source.

A ready-made version

WaMenu is a self-hosted PHP/MySQL QR menu I make at Murabyte: Arabic (RTL) and English customer views, table QR cards, a cart with notes, server-side price and availability rechecks, and a WhatsApp order draft the customer sends. No online payments and no automatic sending. Hosting is separate. Regular license $29: https://murabyte.gumroad.com/l/wamenu-qr-menu

php #whatsapp #webdev #restaurant #murabyte

Written with AI assistance and reviewed by Murabyte.

Top comments (0)