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 |