This script is designed to simplify the order confirmation process by allowing Shopify store owners to send customers a personalized WhatsApp message with two buttons: Confirm and Cancel. Each button contains a dynamic URL with order details (order ID, customer name, product name, and amount). When the customer clicks either button, they are redirected to a confirmation page that logs the response and updates the order on Shopify by adding a specific tag — either “Confirmed by Customer via WhatsApp” or “Canceled by Customer via WhatsApp.” This helps store owners track customer actions directly within their Shopify admin and manage orders more efficiently, all without requiring manual input or login from the customer side.
<?php // Get URL parameters $order_id = $_GET['id'] ?? ''; $action = $_GET['action'] ?? ''; $customer_name = $_GET['customer_name'] ?? ''; $product_name = $_GET['product_name'] ?? ''; $total_amount = $_GET['total_amount'] ?? ''; // Shopify credentials $access_token = "shpat_9---------------"; $shop_domain = "---------------"; // ✅ must be .myshopify.com $api_version = "2023-10"; // Update if needed // Generate tag based on action $tag_to_add = $action === "confirm" ? "Confirmed via WhatsApp" : ($action === "cancel" ? "Canceled via WhatsApp" : ''); // Exit if invalid if (!$order_id || !$tag_to_add) { die("❌ Invalid order ID or action."); } // Step 1: Get existing order tags $order_url = "https://$shop_domain/admin/api/$api_version/orders/$order_id.json"; $ch = curl_init($order_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "X-Shopify-Access-Token: $access_token", "Content-Type: application/json" ]); $response = curl_exec($ch); $order_data = json_decode($response, true); curl_close($ch); if (!isset($order_data['order'])) { die("❌ Unable to fetch order. Response: $response"); } // Step 2: Merge new tag with existing ones $current_tags = explode(',', $order_data['order']['tags'] ?? ''); $current_tags = array_map('trim', $current_tags); $current_tags[] = $tag_to_add; $unique_tags = implode(', ', array_unique($current_tags)); // Step 3: Update order with merged tags $update_url = "https://$shop_domain/admin/api/$api_version/orders/$order_id.json"; $update_data = json_encode([ "order" => [ "id" => $order_id, "tags" => $unique_tags ] ]); $ch = curl_init($update_url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $update_data); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "X-Shopify-Access-Token: $access_token", "Content-Type: application/json" ]); $update_response = curl_exec($ch); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // Log the response $log = "[" . date("Y-m-d H:i:s") . "] OrderID: $order_id | Action: $action | Shopify Status: $http_status" . PHP_EOL; file_put_contents("confirmation_log.txt", $log, FILE_APPEND); // Output message to customer if ($action === "confirm") { echo "✅ Thank you, $customer_name!
Your order for $product_name (Rs. $total_amount) has been confirmed.
"; } elseif ($action === "cancel") { echo "❌ Hello $customer_name,
Your order for $product_name (Rs. $total_amount) has been canceled.
"; } else { echo "