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 |