PluginProbe ʕ •ᴥ•ʔ
Meta for WooCommerce / 3.6.2
Meta for WooCommerce v3.6.2
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 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