From 20faf2cb4e5f2ce09b1aa9c4cd3360aa145893a7 Mon Sep 17 00:00:00 2001 From: Angelo Ou Date: Thu, 29 May 2025 15:14:53 -0400 Subject: [PATCH 1/2] Update product validation logic for checkout --- includes/Checkout.php | 96 ++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 38 deletions(-) diff --git a/includes/Checkout.php b/includes/Checkout.php index 9d594e881..5bfb809c7 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,31 +93,51 @@ 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', + 'flow_name' => 'checkout', + 'flow_step' => 'add_to_cart', '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( - 'Failed to add product to cart', + $error_message, array( 'flow_name' => 'checkout', + 'flow_step' => 'product_quantity_validation', 'incoming_params' => array( 'products_param' => $products_param, 'product_id' => $product_id, + 'quantity' => $quantity, ), ) ); @@ -132,33 +152,33 @@ public function load_checkout_permalink_template( $template ) { $checkout_url = wc_get_checkout_url(); echo ' - - - - - Checkout - - - - - - '; + + + + + Checkout + + + + + + '; exit; } From e1b2078083fbaf37c455f605ea91a75f7d261810 Mon Sep 17 00:00:00 2001 From: Angelo Ou Date: Thu, 29 May 2025 15:59:26 -0400 Subject: [PATCH 2/2] Fix extra data --- includes/Checkout.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/includes/Checkout.php b/includes/Checkout.php index 5bfb809c7..4c940b2a5 100644 --- a/includes/Checkout.php +++ b/includes/Checkout.php @@ -112,13 +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', - 'incoming_params' => array( + 'flow_name' => 'checkout', + 'flow_step' => 'add_to_cart', + 'extra_data' => [ 'products_param' => $products_param, 'product_id' => $product_id, 'quantity' => $quantity, - ), + ], ) ); } @@ -132,13 +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', - 'incoming_params' => array( + 'flow_name' => 'checkout', + 'flow_step' => 'product_quantity_validation', + 'extra_data' => [ 'products_param' => $products_param, 'product_id' => $product_id, 'quantity' => $quantity, - ), + ], ) ); }