Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lifehome/11fb858c6bb7cb6aa312b81423270d6b to your computer and use it in GitHub Desktop.
Save lifehome/11fb858c6bb7cb6aa312b81423270d6b to your computer and use it in GitHub Desktop.
Very dirty patch to add Whatsapp contact button, Hong Kong area code hardcoded
From f9482c5c0c2da037acf3c08942be711df630dc95 Mon Sep 17 00:00:00 2001
From: Ivan Ip <m@lifeho.me>
Date: Thu, 29 Apr 2021 23:07:42 +0800
Subject: [PATCH] Adhoc patch to add Whatsapp button for contacting customer
---
.../BillingInformationViewController.swift | 48 +++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Customer Section/Billing Information/BillingInformationViewController.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Customer Section/Billing Information/BillingInformationViewController.swift
index 580e5f9b6..78cff562d 100644
--- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Customer Section/Billing Information/BillingInformationViewController.swift
+++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Customer Section/Billing Information/BillingInformationViewController.swift
@@ -123,6 +123,12 @@ private extension BillingInformationViewController {
self.displayMessageComposerIfPossible(from: self)
}
+ actionSheet.addDefaultActionWithTitle("Whatsapp") { [weak self] _ in
+ guard let self = self else { return }
+ ServiceLocator.analytics.track(.orderDetailCustomerSMSOptionTapped)
+ self.displayWhatsappComposerIfPossible(from: self)
+ }
+
let popoverController = actionSheet.popoverPresentationController
popoverController?.sourceView = sourceView
popoverController?.sourceRect = sourceView.bounds
@@ -160,6 +166,48 @@ private extension BillingInformationViewController {
"type": "sms"])
}
+ /// Initiate communication with a customer via Whatsapp
+ func displayWhatsappComposerIfPossible(from: UIViewController) {
+ guard let phoneNumber = order.billingAddress?.cleanedPhoneNumber else {
+ return
+ }
+
+ let whatsappNumber: String
+ if phoneNumber.characterCount == 8 {
+ whatsappNumber = "+852\(phoneNumber)"
+ } else {
+ whatsappNumber = phoneNumber
+ }
+
+ let urlWhats = "whatsapp://send?phone=\(whatsappNumber)"
+
+ let title = "Error"
+ let alertController = UIAlertController(title: title,
+ message: "Whatsapp is not installed.",
+ preferredStyle: .alert)
+ let cancel = UIAlertAction(title: "OK",
+ style: .cancel,
+ handler: nil)
+ alertController.addAction(cancel)
+
+ if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
+ if let whatsappURL = NSURL(string: urlString) {
+ if UIApplication.shared.canOpenURL(whatsappURL as URL) {
+ UIApplication.shared.open(whatsappURL as URL, options: [:], completionHandler: { (Bool) in
+ })
+ } else {
+ present(alertController, animated: true)
+ }
+ }
+ }
+
+ ServiceLocator.analytics.track(.orderContactAction, withProperties: [
+ "id": order.orderID,
+ "status": order.status.rawValue,
+ "type": "whatsapp"
+ ])
+ }
+
/// Create an action sheet that offers the option to copy the email address
///
func displayEmailCopyAlert(from sourceView: UIView) {
--
2.30.1 (Apple Git-130)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment