From 3de4ce443927f6f2026f22ef9cc6df06d5402dce Mon Sep 17 00:00:00 2001 From: Angelo Ou Date: Thu, 29 May 2025 19:40:19 -0400 Subject: [PATCH 1/3] Add logging for coupon in checkout --- includes/Admin.php | 2 +- includes/Checkout.php | 128 +++++++++++++++++++++--------------------- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/includes/Admin.php b/includes/Admin.php index f8f4d963a..2005c67bf 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -1610,7 +1610,7 @@ public function save_product_variation_edit_fields( $variation_id, $index ) { } // ALWAYS save Facebook field data (this fixes the PR #2931 breaking change) - $posted_param = 'variable_' . \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION; + $posted_param = 'variable_' . \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Intentionally getting raw value to apply different sanitization methods below $description_raw = isset( $_POST[ $posted_param ][ $index ] ) ? wp_unslash( $_POST[ $posted_param ][ $index ] ) : null; diff --git a/includes/Checkout.php b/includes/Checkout.php index 4c940b2a5..06c430c3b 100644 --- a/includes/Checkout.php +++ b/includes/Checkout.php @@ -84,7 +84,7 @@ public function load_checkout_permalink_template( $template ) { $products = explode( ',', $products_param ); foreach ( $products as $product ) { - list( $product_id, $quantity ) = explode( ':', $product ); + list($product_id, $quantity) = explode( ':', $product ); // Parse the product ID. The input is sent in the Retailer ID format (see get_fb_retailer_id()) // The Retailer ID format is: {product_sku}_{product_id}, so we need to extract the product_id @@ -93,52 +93,32 @@ public function load_checkout_permalink_template( $template ) { $product_id = end( $parts ); } - $product_obj = wc_get_product( $product_id ); - - if ( - $product_obj && - $product_obj->is_purchasable() && - is_numeric( $quantity ) && - $quantity > 0 - ) { - $added = WC()->cart->add_to_cart( $product_id, $quantity ); - if ( ! $added ) { - $error_message = sprintf( - 'WC add_to_cart() failed: product_id=%s, quantity=%s', - $product_id, - $quantity - ); - - \WC_Facebookcommerce_Utils::log_to_meta( - $error_message, + // Validate and add the product to the cart + if ( is_numeric( $product_id ) && is_numeric( $quantity ) && $quantity > 0 ) { + try { + WC()->cart->add_to_cart( $product_id, $quantity ); + } catch ( \Exception $e ) { + \WC_Facebookcommerce_Utils::log_exception_immediately_to_meta( + $e, array( - 'flow_name' => 'checkout', - 'flow_step' => 'add_to_cart', - 'extra_data' => [ + 'event' => 'checkout', + 'event_type' => 'checkout_permalink_template_exception', + 'incoming_params' => array( 'products_param' => $products_param, 'product_id' => $product_id, - 'quantity' => $quantity, - ], + ), ) ); } } else { - $error_message = sprintf( - 'Invalid product or quantity: product_id=%s, quantity=%s', - $product_id, - $quantity - ); - \WC_Facebookcommerce_Utils::log_to_meta( - $error_message, + 'Failed to add product to cart', array( - 'flow_name' => 'checkout', - 'flow_step' => 'product_quantity_validation', - 'extra_data' => [ + 'flow_name' => 'checkout', + 'incoming_params' => array( 'products_param' => $products_param, 'product_id' => $product_id, - 'quantity' => $quantity, - ], + ), ) ); } @@ -147,38 +127,58 @@ public function load_checkout_permalink_template( $template ) { $coupon_code = get_query_var( 'coupon' ); if ( $coupon_code ) { - WC()->cart->apply_coupon( sanitize_text_field( $coupon_code ) ); + $coupon_code_sanitized = sanitize_text_field( $coupon_code ); + WC()->cart->apply_coupon( $coupon_code_sanitized ); + + if ( ! in_array( $coupon_code_sanitized, WC()->cart->get_applied_coupons(), true ) ) { + $error_message = sprintf( + 'Failed to apply coupon: %s', + $coupon_code_sanitized + ); + + \WC_Facebookcommerce_Utils::log_to_meta( + $error_message, + array( + 'flow_name' => 'checkout', + 'flow_step' => 'apply_coupon', + 'extra_data' => [ + 'coupon_param' => $coupon_code, + 'coupon_code' => $coupon_code_sanitized, + ], + ) + ); + } } $checkout_url = wc_get_checkout_url(); echo ' - - - - - Checkout - - - - - - '; + + + + + Checkout + + + + + + '; exit; } From f2c5128a10481ca67369d89f5246c402859c448f Mon Sep 17 00:00:00 2001 From: Angelo Ou Date: Thu, 29 May 2025 19:43:28 -0400 Subject: [PATCH 2/3] Merge main --- includes/Checkout.php | 104 +++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/includes/Checkout.php b/includes/Checkout.php index 06c430c3b..282e8679d 100644 --- a/includes/Checkout.php +++ b/includes/Checkout.php @@ -84,7 +84,7 @@ public function load_checkout_permalink_template( $template ) { $products = explode( ',', $products_param ); foreach ( $products as $product ) { - list($product_id, $quantity) = explode( ':', $product ); + list( $product_id, $quantity ) = explode( ':', $product ); // Parse the product ID. The input is sent in the Retailer ID format (see get_fb_retailer_id()) // The Retailer ID format is: {product_sku}_{product_id}, so we need to extract the product_id @@ -93,32 +93,44 @@ public function load_checkout_permalink_template( $template ) { $product_id = end( $parts ); } - // Validate and add the product to the cart - if ( is_numeric( $product_id ) && is_numeric( $quantity ) && $quantity > 0 ) { - try { - WC()->cart->add_to_cart( $product_id, $quantity ); - } catch ( \Exception $e ) { - \WC_Facebookcommerce_Utils::log_exception_immediately_to_meta( - $e, + $product_obj = wc_get_product( $product_id ); + + if ( + $product_obj && + $product_obj->is_purchasable() && + is_numeric( $quantity ) && + $quantity > 0 + ) { + $added = WC()->cart->add_to_cart( $product_id, $quantity ); + if ( ! $added ) { + $error_message = sprintf( + 'WC add_to_cart() failed: product_id=%s, quantity=%s', + $product_id, + $quantity + ); + + \WC_Facebookcommerce_Utils::log_to_meta( + $error_message, array( - 'event' => 'checkout', - 'event_type' => 'checkout_permalink_template_exception', - 'incoming_params' => array( - 'products_param' => $products_param, - 'product_id' => $product_id, - ), + 'flow_name' => 'checkout', + 'flow_step' => 'add_to_cart', + 'extra_data' => [ 'products_param' => $products_param, 'product_id' => $product_id, 'quantity' => $quantity, ], ) ); } } else { + $error_message = sprintf( + 'Invalid product or quantity: product_id=%s, quantity=%s', + $product_id, + $quantity + ); + \WC_Facebookcommerce_Utils::log_to_meta( - 'Failed to add product to cart', + $error_message, array( 'flow_name' => 'checkout', - 'incoming_params' => array( - 'products_param' => $products_param, - 'product_id' => $product_id, - ), + 'flow_step' => 'product_quantity_validation', + 'extra_data' => [ 'products_param' => $products_param, 'product_id' => $product_id, 'quantity' => $quantity, ], ) ); } @@ -152,33 +164,33 @@ public function load_checkout_permalink_template( $template ) { $checkout_url = wc_get_checkout_url(); echo ' - - - - - Checkout - - - - - - '; + + + + + Checkout + + + + + + '; exit; } From 3090c1b9d65ccaecc5d491a97744578994de883c Mon Sep 17 00:00:00 2001 From: Angelo Ou Date: Thu, 29 May 2025 19:44:37 -0400 Subject: [PATCH 3/3] Fix linter errors --- includes/Checkout.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/includes/Checkout.php b/includes/Checkout.php index 282e8679d..749cc556b 100644 --- a/includes/Checkout.php +++ b/includes/Checkout.php @@ -112,9 +112,13 @@ public function load_checkout_permalink_template( $template ) { \WC_Facebookcommerce_Utils::log_to_meta( $error_message, array( - 'flow_name' => 'checkout', - 'flow_step' => 'add_to_cart', - 'extra_data' => [ 'products_param' => $products_param, 'product_id' => $product_id, 'quantity' => $quantity, ], + 'flow_name' => 'checkout', + 'flow_step' => 'add_to_cart', + 'extra_data' => [ + 'products_param' => $products_param, + 'product_id' => $product_id, + 'quantity' => $quantity, + ], ) ); } @@ -128,9 +132,13 @@ public function load_checkout_permalink_template( $template ) { \WC_Facebookcommerce_Utils::log_to_meta( $error_message, array( - 'flow_name' => 'checkout', - 'flow_step' => 'product_quantity_validation', - 'extra_data' => [ 'products_param' => $products_param, 'product_id' => $product_id, 'quantity' => $quantity, ], + 'flow_name' => 'checkout', + 'flow_step' => 'product_quantity_validation', + 'extra_data' => [ + 'products_param' => $products_param, + 'product_id' => $product_id, + 'quantity' => $quantity, + ], ) ); }