facebook-for-woocommerce
Last commit date
assets
3 months ago
data
1 year ago
i18n
3 months ago
includes
3 months ago
vendor
3 months ago
LICENSE
7 years ago
changelog.txt
3 months ago
class-wc-facebookcommerce.php
4 months ago
facebook-commerce-admin-notice.php
4 months ago
facebook-commerce-events-tracker.php
3 months ago
facebook-commerce-iframe-whatsapp-utility-event.php
3 months ago
facebook-commerce-pixel-event.php
4 months ago
facebook-commerce.php
4 months ago
facebook-config-warmer.php
4 months ago
facebook-for-woocommerce.php
3 months ago
playwright.config.js
4 months ago
readme.txt
3 months ago
facebook-commerce-iframe-whatsapp-utility-event.php
126 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 MetaCommerce |
| 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 | add_action( 'woocommerce_order_status_changed', array( $this, 'process_wc_order_status_changed' ), 10, 3 ); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Hook to process Order Processing, Order Completed and Order Refunded events for WhatsApp Utility Messages |
| 39 | * |
| 40 | * @param string $order_id Order id |
| 41 | * @param string $old_status Old Order Status |
| 42 | * @param string $new_status New Order Status |
| 43 | * |
| 44 | * @return void |
| 45 | * @since 2.3.0 |
| 46 | */ |
| 47 | public function process_wc_order_status_changed( $order_id, $old_status, $new_status ) { |
| 48 | $supported_statuses = array_keys( self::ORDER_STATUS_TO_EVENT_MAPPING ); |
| 49 | if ( ! in_array( $new_status, $supported_statuses, true ) ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | wc_get_logger()->info( |
| 54 | sprintf( |
| 55 | /* translators: %s $order_id */ |
| 56 | __( 'Processing Order id %1$s to send Whatsapp Utility messages', 'facebook-for-woocommerce' ), |
| 57 | $order_id, |
| 58 | ) |
| 59 | ); |
| 60 | $event = self::ORDER_STATUS_TO_EVENT_MAPPING[ $new_status ]; |
| 61 | $order = wc_get_order( $order_id ); |
| 62 | $order_details_link = $order->get_checkout_order_received_url(); |
| 63 | // Get WhatsApp Phone number from entered Billing and Shipping phone number |
| 64 | $billing_phone_number = $order->get_billing_phone(); |
| 65 | $shipping_phone_number = $order->get_shipping_phone(); |
| 66 | $phone_number = $billing_phone_number ?? $shipping_phone_number; |
| 67 | // Get Country Code from Billing and Shipping Country to override Country Calling Code |
| 68 | $should_use_billing_info = ! empty( $billing_phone_number ); |
| 69 | $country_code = $should_use_billing_info ? $order->get_billing_country() : $order->get_shipping_country(); |
| 70 | // Get Customer first name |
| 71 | $first_name = $order->get_billing_first_name(); |
| 72 | // Get Total Refund Amount for Order Refunded event |
| 73 | $total_refund = 0; |
| 74 | foreach ( $order->get_refunds() as $refund ) { |
| 75 | $total_refund += $refund->get_amount(); |
| 76 | } |
| 77 | $currency = $order->get_currency(); |
| 78 | $refund_amount = $total_refund * 1000; |
| 79 | if ( empty( $phone_number ) || empty( $event ) || empty( $first_name ) ) { |
| 80 | wc_get_logger()->info( |
| 81 | sprintf( |
| 82 | /* translators: %s $order_id */ |
| 83 | __( 'Customer Events Post API call for Order id %1$s skipped due to missing Order info', 'facebook-for-woocommerce' ), |
| 84 | $order_id, |
| 85 | ) |
| 86 | ); |
| 87 | return; |
| 88 | } |
| 89 | // Always build order metadata for rich order status |
| 90 | $order_metadata = self::build_order_metadata( $order, $currency ); |
| 91 | |
| 92 | WhatsAppExtension::process_whatsapp_utility_message_event( $this->plugin, $event, $order_id, $order_details_link, $phone_number, $first_name, $refund_amount, $currency, $country_code, $order_metadata ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Build order metadata from WooCommerce order object. |
| 97 | * |
| 98 | * @param \WC_Order $order The WooCommerce order object. |
| 99 | * @param string $currency The order currency code. |
| 100 | * @return array Order metadata with keys: order_url, order_date, currency, shipping_method, items. |
| 101 | */ |
| 102 | private static function build_order_metadata( $order, $currency ) { |
| 103 | $order_metadata = array( |
| 104 | 'order_url' => ( method_exists( $order, 'get_view_order_url' ) ? $order->get_view_order_url() : $order->get_checkout_order_received_url() ), |
| 105 | 'order_date' => ( method_exists( $order, 'get_date_created' ) ? ( $order->get_date_created() ? $order->get_date_created()->date( 'c' ) : '' ) : '' ), |
| 106 | 'currency' => $currency, |
| 107 | 'shipping_method' => ( method_exists( $order, 'get_shipping_method' ) ? $order->get_shipping_method() : '' ), |
| 108 | 'items' => array(), |
| 109 | ); |
| 110 | |
| 111 | $items = method_exists( $order, 'get_items' ) ? $order->get_items() : array(); |
| 112 | foreach ( $items as $item ) { |
| 113 | $product_id = method_exists( $item, 'get_product_id' ) ? $item->get_product_id() : ( isset( $item['product_id'] ) ? $item['product_id'] : 0 ); |
| 114 | $item_total = method_exists( $item, 'get_total' ) ? $item->get_total() : ( isset( $item['line_total'] ) ? $item['line_total'] : 0 ); |
| 115 | $order_metadata['items'][] = array( |
| 116 | 'product_id' => $product_id, |
| 117 | 'name' => method_exists( $item, 'get_name' ) ? $item->get_name() : ( isset( $item['name'] ) ? $item['name'] : '' ), |
| 118 | 'quantity' => method_exists( $item, 'get_quantity' ) ? $item->get_quantity() : ( isset( $item['qty'] ) ? $item['qty'] : null ), |
| 119 | 'amount' => $item_total, |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | return $order_metadata; |
| 124 | } |
| 125 | } |
| 126 |