From b19ad2f6d7283b4a3d475d60a178dd6a1fc4c224 Mon Sep 17 00:00:00 2001 From: "Jorge A. Torres" Date: Thu, 16 Mar 2023 08:42:01 -0300 Subject: [PATCH] Prevent possible PHP warnings produced by 'woocommerce_ajax_order_items_removed' hook (#37178) * Remove superfluous check. * Prevent PHP warnings in `woocommerce_ajax_order_items_removed` * Add changelog --- plugins/woocommerce/changelog/fix-32717 | 4 ++++ plugins/woocommerce/includes/class-wc-ajax.php | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-32717 diff --git a/plugins/woocommerce/changelog/fix-32717 b/plugins/woocommerce/changelog/fix-32717 new file mode 100644 index 00000000000..7203612332a --- /dev/null +++ b/plugins/woocommerce/changelog/fix-32717 @@ -0,0 +1,4 @@ +Significance: patch +Type: tweak + +Prevent 'woocommerce_ajax_order_items_removed' from generating PHP warnings. diff --git a/plugins/woocommerce/includes/class-wc-ajax.php b/plugins/woocommerce/includes/class-wc-ajax.php index e291c78a2a0..b0e39aae036 100644 --- a/plugins/woocommerce/includes/class-wc-ajax.php +++ b/plugins/woocommerce/includes/class-wc-ajax.php @@ -1315,7 +1315,7 @@ class WC_AJAX { 'city' => isset( $_POST['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $_POST['city'] ) ) ) : '', ); - if ( ! is_array( $order_item_ids ) && is_numeric( $order_item_ids ) ) { + if ( is_numeric( $order_item_ids ) ) { $order_item_ids = array( $order_item_ids ); } @@ -1332,6 +1332,10 @@ class WC_AJAX { $item_id = absint( $item_id ); $item = $order->get_item( $item_id ); + if ( ! $item ) { + continue; + } + // Before deleting the item, adjust any stock values already reduced. if ( $item->is_type( 'line_item' ) ) { $changed_stock = wc_maybe_adjust_line_item_product_stock( $item, 0 ); @@ -1363,7 +1367,7 @@ class WC_AJAX { * @param bool|array|WP_Error $changed_store Result of wc_maybe_adjust_line_item_product_stock(). * @param bool|WC_Order|WC_Order_Refund $order As returned by wc_get_order(). */ - do_action( 'woocommerce_ajax_order_items_removed', $item_id, $item, $changed_stock, $order ); + do_action( 'woocommerce_ajax_order_items_removed', $item_id ?? 0, $item ?? false, $changed_stock ?? false, $order ); // Get HTML to return. ob_start();