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-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-whatsapp-utility-event.php
178 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\WhatsAppUtilityConnection;
12
13 /**
14 * Event Processor for sending WhatsApp Utility Message when Order Management events are triggered
15 */
16 class WC_Facebookcommerce_Whatsapp_Utility_Event {
17
18 /** @var array Mapping of Order Status to Event name */
19 const ORDER_STATUS_TO_EVENT_MAPPING = array(
20 'processing' => 'ORDER_PLACED',
21 'completed' => 'ORDER_FULFILLED',
22 'refunded' => 'ORDER_REFUNDED',
23 );
24
25 public function __construct() {
26 if ( ! $this->is_whatsapp_utility_enabled() ) {
27 return;
28 }
29 add_action( 'woocommerce_order_status_changed', array( $this, 'process_wc_order_status_changed' ), 10, 3 );
30 }
31
32 /**
33 * Determines if WhatsApp Utility Messages are enabled
34 * TODO: Update this function to check for gating logic for Alpha businesses
35 *
36 * @since 2.3.0
37 *
38 * @return bool
39 */
40 private function is_whatsapp_utility_enabled() {
41 return false;
42 }
43
44
45 /**
46 * Hook to process Order Processing, Order Completed and Order Refunded events for WhatsApp Utility Messages
47 *
48 * @param string $order_id Order id
49 * @param string $old_status Old Order Status
50 * @param string $new_status New Order Status
51 *
52 * @return void
53 * @since 2.3.0
54 */
55 public function process_wc_order_status_changed( $order_id, $old_status, $new_status ) {
56 // WhatsApp Utility Messages are supported only for Processing status
57 $supported_statuses = array_keys( self::ORDER_STATUS_TO_EVENT_MAPPING );
58 if ( ! in_array( $new_status, $supported_statuses, true ) ) {
59 return;
60 }
61
62 wc_get_logger()->info(
63 sprintf(
64 /* translators: %s $order_id */
65 __( 'Processing Order id %1$s to send Whatsapp Utility messages', 'facebook-for-woocommerce' ),
66 $order_id,
67 )
68 );
69 $event = self::ORDER_STATUS_TO_EVENT_MAPPING[ $new_status ];
70
71 // Check WhatsApp Event Config is active
72 $event_config_id_option_name = implode( '_', array( WhatsAppUtilityConnection::WA_UTILITY_OPTION_PREFIX, strtolower( $event ), 'event_config_id' ) );
73 $event_config_language_option_name = implode( '_', array( WhatsAppUtilityConnection::WA_UTILITY_OPTION_PREFIX, strtolower( $event ), 'language' ) );
74 $event_config_id = get_option( $event_config_id_option_name, null );
75 $language_code = get_option( $event_config_language_option_name, null );
76 if ( empty( $event_config_id ) || empty( $language_code ) ) {
77 wc_get_logger()->info(
78 sprintf(
79 /* translators: %s $order_id */
80 __( 'Messages Post API call for Order id %1$s skipped due to no active event config', 'facebook-for-woocommerce' ),
81 $order_id,
82 )
83 );
84 return;
85 }
86
87 $order = wc_get_order( $order_id );
88 // Check WhatsApp Consent Checkbox is selected in shipping or billing
89 $user_wa_consent = $this->has_billing_or_shipping_number_whatsapp_consent( $order );
90 $wa_billing_consent_enabled = $user_wa_consent['has_user_consented_to_wa_billing_number_notif'];
91 $wa_shipping_consent_enabled = $user_wa_consent['has_user_consented_to_wa_shipping_number_notif'];
92
93 $has_whatsapp_consent = $wa_billing_consent_enabled || $wa_shipping_consent_enabled;
94 $should_use_billing_info = isset( $billing_phone_number ) && $wa_billing_consent_enabled;
95 // Get WhatsApp Phone number from entered Billing and Shipping phone number
96 $billing_phone_number = $order->get_billing_phone();
97 $shipping_phone_number = $order->get_shipping_phone();
98 $phone_number = $should_use_billing_info ? $billing_phone_number : $shipping_phone_number;
99 // Get Country Code from Billing and Shipping Country to override Country Calling Code
100 $country_code = $should_use_billing_info ? $order->get_billing_country() : $order->get_shipping_country();
101 // Get Customer first name
102 $first_name = $order->get_billing_first_name();
103 // Get Total Refund Amount for Order Refunded event
104 $total_refund = 0;
105 foreach ( $order->get_refunds() as $refund ) {
106 $total_refund += $refund->get_amount();
107 }
108 $currency = $order->get_currency();
109 $refund_amount = $total_refund * 1000;
110 if ( empty( $phone_number ) || ! $has_whatsapp_consent || empty( $event ) || empty( $first_name ) ) {
111 wc_get_logger()->info(
112 sprintf(
113 /* translators: %s $order_id */
114 __( 'Messages Post API call for Order id %1$s skipped due to missing whatsapp consent or Order info', 'facebook-for-woocommerce' ),
115 $order_id,
116 )
117 );
118 return;
119 }
120
121 // Check Access token and WACS is available
122 $bisu_token = get_option( 'wc_facebook_wa_integration_bisu_access_token', null );
123 $wacs_id = get_option( 'wc_facebook_wa_integration_wacs_id', null );
124 if ( empty( $bisu_token ) || empty( $wacs_id ) ) {
125 wc_get_logger()->info(
126 sprintf(
127 /* translators: %s $order_id */
128 __( 'Messages Post API call for Order id %1$s Failed due to missing access token or wacs info', 'facebook-for-woocommerce' ),
129 $order_id,
130 )
131 );
132 return;
133 }
134 WhatsAppUtilityConnection::post_whatsapp_utility_messages_events_call( $event, $event_config_id, $language_code, $wacs_id, $order_id, $phone_number, $first_name, $refund_amount, $currency, $bisu_token, $country_code );
135 }
136
137 /**
138 * Determines if WhatsApp Consent is Enabled for a user either for Billing or Shipping Phone Number for Blocks or Classic Flows
139 *
140 * @param \WC_Order $order Order object
141 * @since 2.3.0
142 *
143 * @return array
144 */
145 private function has_billing_or_shipping_number_whatsapp_consent( $order ) {
146 $block_billing_consent_value = $order->get_meta( '_wc_billing/wc_facebook/whatsapp_consent_checkbox' );
147 $block_shipping_consent_value = $order->get_meta( '_wc_shipping/wc_facebook/whatsapp_consent_checkbox' );
148 $classic_billing_consent_value = $order->get_meta( '_billing_whatsapp_consent' );
149 $classic_shipping_consent_value = $order->get_meta( '_shipping_whatsapp_consent' );
150
151 $has_user_consented_to_wa_billing_number_notif = false;
152 $has_user_consented_to_wa_shipping_number_notif = false;
153 if ( $block_billing_consent_value || $classic_billing_consent_value ) {
154 $has_user_consented_to_wa_billing_number_notif = true;
155 }
156
157 if ( $block_shipping_consent_value || $classic_shipping_consent_value ) {
158 $has_user_consented_to_wa_shipping_number_notif = true;
159 }
160
161 wc_get_logger()->info(
162 sprintf(
163 /* translators: %s consent for billing and shipping */
164 __( 'WhatsApp Consent info for user $block_billing_consent_value: %1$s, $block_shipping_consent_value: %2$s, $classic_billing_consent_value: %3$s, $classic_shipping_consent_value: %4$s', 'facebook-for-woocommerce' ),
165 $block_billing_consent_value,
166 $block_shipping_consent_value,
167 $classic_billing_consent_value,
168 $classic_shipping_consent_value
169 )
170 );
171
172 return array(
173 'has_user_consented_to_wa_billing_number_notif' => $has_user_consented_to_wa_billing_number_notif,
174 'has_user_consented_to_wa_shipping_number_notif' => $has_user_consented_to_wa_shipping_number_notif,
175 );
176 }
177 }
178