From 280c813be4d1144dcdab1bb6d09c3027a2a8043b Mon Sep 17 00:00:00 2001 From: Rodrigue Tusse Date: Tue, 3 Dec 2024 14:47:50 +0200 Subject: [PATCH 1/3] Fix: Uncaught ValueError: array_rand() called with null arg. --- includes/Generator/OrderAttribution.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/includes/Generator/OrderAttribution.php b/includes/Generator/OrderAttribution.php index 47e4552..41c85f0 100644 --- a/includes/Generator/OrderAttribution.php +++ b/includes/Generator/OrderAttribution.php @@ -24,11 +24,16 @@ public static function add_order_attribution_meta( $order, $assoc_args = array() return; } + $order_products = $order->get_items(); + + if ( empty( $order_products ) ) { + return; + } + $device_type = self::get_random_device_type(); $source = 'woo.com'; $source_type = self::get_source_type(); $origin = self::get_origin( $source_type, $source ); - $order_products = $order->get_items(); $product_url = get_permalink( $order_products[ array_rand( $order_products ) ]->get_id() ); $utm_content = [ '/', 'campaign_a', 'campaign_b' ]; $utm_content = $utm_content[ array_rand( $utm_content ) ]; From bdd215f168660e7db875a5fb37337cdb7ac5b84d Mon Sep 17 00:00:00 2001 From: Rodrigue Tusse Date: Tue, 3 Dec 2024 14:49:56 +0200 Subject: [PATCH 2/3] Fix get_random_device_type() to match OrderAttributionMeta::get_device_type() possible return value. --- includes/Generator/OrderAttribution.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/includes/Generator/OrderAttribution.php b/includes/Generator/OrderAttribution.php index 41c85f0..a856d3f 100644 --- a/includes/Generator/OrderAttribution.php +++ b/includes/Generator/OrderAttribution.php @@ -222,26 +222,19 @@ public static function get_source( $source_type ) { /** * Get random device type based on the following distribution: * Mobile: 50% - * Desktop: 30% - * Tablet: 10% - * Unknown: 10% + * Desktop: 35% + * Tablet: 15% */ public static function get_random_device_type() { $randomNumber = wp_rand( 1, 100 ); // Generate a random number between 1 and 100. if ( $randomNumber <= 50 ) { return 'Mobile'; - } - - if ( $randomNumber <= 80 ) { + } elseif ( $randomNumber <= 85 ) { return 'Desktop'; - } - - if ( $randomNumber <= 90 ) { + } else { return 'Tablet'; } - - return 'Unknown'; } /** From 4a9a6c1a4997791a52c4b7011b6f549442403984 Mon Sep 17 00:00:00 2001 From: Rodrigue Tusse Date: Tue, 3 Dec 2024 14:50:36 +0200 Subject: [PATCH 3/3] Ensure strtoupper is not called with '' argument. --- includes/Generator/CustomerInfo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Generator/CustomerInfo.php b/includes/Generator/CustomerInfo.php index 4357149..3725b14 100644 --- a/includes/Generator/CustomerInfo.php +++ b/includes/Generator/CustomerInfo.php @@ -16,7 +16,7 @@ class CustomerInfo { * @return string|\WP_Error */ public static function get_valid_country_code( ?string $country_code = '' ) { - $country_code = strtoupper( $country_code ); + $country_code = !empty( $country_code ) ? strtoupper( $country_code ) : ''; if ( $country_code && ! WC()->countries->country_exists( $country_code ) ) { $country_code = new \WP_Error(