PluginProbe ʕ •ᴥ•ʔ
Meta for WooCommerce / 3.5.12
Meta for WooCommerce v3.5.12
3.7.3 3.7.2 3.7.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.9.11 1.9.12 1.9.13 1.9.14 1.9.15 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.5.0 2.5.1 2.6.0 2.6.1 2.6.10 2.6.11 2.6.12 2.6.13 2.6.14 2.6.15 2.6.16 2.6.17 2.6.18 2.6.19 2.6.2 2.6.20 2.6.21 2.6.22 2.6.23 2.6.24 2.6.25 2.6.26 2.6.27 2.6.28 2.6.29 2.6.3 2.6.30 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.18 3.0.19 3.0.2 3.0.20 3.0.21 3.0.22 3.0.23 3.0.24 3.0.25 3.0.26 3.0.27 3.0.28 3.0.29 3.0.3 3.0.30 3.0.31 3.0.32 3.0.33 3.0.34 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.4.0 3.4.1 3.4.10 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.10 3.5.11 3.5.12 3.5.13 3.5.14 3.5.15 3.5.16 3.5.17 3.5.18 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0
facebook-for-woocommerce / facebook-commerce-iframe-whatsapp-utility-event.php
facebook-for-woocommerce Last commit date
assets 8 months ago data 1 year ago i18n 8 months ago includes 8 months ago vendor 8 months ago LICENSE 7 years ago changelog.txt 8 months ago class-wc-facebookcommerce.php 9 months ago facebook-commerce-admin-banner.php 8 months ago facebook-commerce-admin-notice.php 1 year ago facebook-commerce-events-tracker.php 8 months ago facebook-commerce-iframe-whatsapp-utility-event.php 8 months ago facebook-commerce-pixel-event.php 1 year ago facebook-commerce-whatsapp-utility-event.php 9 months ago facebook-commerce.php 8 months ago facebook-config-warmer.php 1 year ago facebook-for-woocommerce.php 8 months ago playwright.config.js 1 year ago readme.txt 8 months ago
facebook-commerce-iframe-whatsapp-utility-event.php
110 lines
1 <?php
2 /**
3 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
4 *
5 * This source code is licensed under the license found in the
6 * LICENSE file in the root directory of this source tree.
7 *
8 * @package FacebookCommerce
9 */
10
11 use WooCommerce\Facebook\Handlers\WhatsAppExtension;
12 use WooCommerce\Facebook\RolloutSwitches;
13
14 /**
15 * Event Processor for sending WhatsApp Utility Message when Order Management events are triggered
16 */
17 class WC_Facebookcommerce_Iframe_Whatsapp_Utility_Event {
18
19 /** @var array Mapping of Order Status to Event name */
20 const ORDER_STATUS_TO_EVENT_MAPPING = array(
21 'processing' => 'ORDER_PLACED',
22 'completed' => 'ORDER_FULFILLED',
23 'refunded' => 'ORDER_REFUNDED',
24 );
25
26 /** @var \WC_Facebookcommerce */
27 private $plugin;
28
29
30 public function __construct( WC_Facebookcommerce $plugin ) {
31 $rollout_switches = $plugin->get_rollout_switches();
32 $this->plugin = $plugin;
33 if ( ! $this->is_whatsapp_utility_enabled() ) {
34 return;
35 }
36 add_action( 'woocommerce_order_status_changed', array( $this, 'process_wc_order_status_changed' ), 10, 3 );
37 }
38
39
40 /**
41 * Determines if WhatsApp Utility Messages are enabled
42 *
43 * @return bool
44 */
45 private function is_whatsapp_utility_enabled() {
46 $is_enabled = false;
47 $rollout_switches = $this->plugin->get_rollout_switches();
48 if ( isset( $rollout_switches ) ) {
49 $is_enabled = $rollout_switches->is_switch_enabled(
50 RolloutSwitches::WHATSAPP_UTILITY_MESSAGING_BETA_EXPERIENCE
51 ) ?? false;
52 }
53 return $is_enabled;
54 }
55
56 /**
57 * Hook to process Order Processing, Order Completed and Order Refunded events for WhatsApp Utility Messages
58 *
59 * @param string $order_id Order id
60 * @param string $old_status Old Order Status
61 * @param string $new_status New Order Status
62 *
63 * @return void
64 * @since 2.3.0
65 */
66 public function process_wc_order_status_changed( $order_id, $old_status, $new_status ) {
67 $supported_statuses = array_keys( self::ORDER_STATUS_TO_EVENT_MAPPING );
68 if ( ! in_array( $new_status, $supported_statuses, true ) ) {
69 return;
70 }
71
72 wc_get_logger()->info(
73 sprintf(
74 /* translators: %s $order_id */
75 __( 'Processing Order id %1$s to send Whatsapp Utility messages', 'facebook-for-woocommerce' ),
76 $order_id,
77 )
78 );
79 $event = self::ORDER_STATUS_TO_EVENT_MAPPING[ $new_status ];
80 $order = wc_get_order( $order_id );
81 $order_details_link = $order->get_checkout_order_received_url();
82 // Get WhatsApp Phone number from entered Billing and Shipping phone number
83 $billing_phone_number = $order->get_billing_phone();
84 $shipping_phone_number = $order->get_shipping_phone();
85 $phone_number = $billing_phone_number ?? $shipping_phone_number;
86 // Get Country Code from Billing and Shipping Country to override Country Calling Code
87 $country_code = $should_use_billing_info ? $order->get_billing_country() : $order->get_shipping_country();
88 // Get Customer first name
89 $first_name = $order->get_billing_first_name();
90 // Get Total Refund Amount for Order Refunded event
91 $total_refund = 0;
92 foreach ( $order->get_refunds() as $refund ) {
93 $total_refund += $refund->get_amount();
94 }
95 $currency = $order->get_currency();
96 $refund_amount = $total_refund * 1000;
97 if ( empty( $phone_number ) || empty( $event ) || empty( $first_name ) ) {
98 wc_get_logger()->info(
99 sprintf(
100 /* translators: %s $order_id */
101 __( 'Customer Events Post API call for Order id %1$s skipped due to missing Order info', 'facebook-for-woocommerce' ),
102 $order_id,
103 )
104 );
105 return;
106 }
107 WhatsAppExtension::process_whatsapp_utility_message_event( $this->plugin, $event, $order_id, $order_details_link, $phone_number, $first_name, $refund_amount, $currency, $country_code );
108 }
109 }
110