From 4df98f6379b78bced32c869d960488c01ff3e973 Mon Sep 17 00:00:00 2001 From: danielvonmitschke Date: Mon, 17 Jun 2024 18:21:20 +0200 Subject: [PATCH] Fire action if order line item has been restored (#40848) * Fire action if order line item has been restored This is the equivalent to the 'woocommerce_reduce_order_item_stock' hook in the wc_reduce_stock_levels() function. * Add @since tag to docblock * Change position of @since tag * Add changelog * Use tabs for indents * Fix indents again * Fix another indent --- .../changelog/2024-06-14-18-11-56-915980 | 4 ++++ .../woocommerce/includes/wc-stock-functions.php | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/2024-06-14-18-11-56-915980 diff --git a/plugins/woocommerce/changelog/2024-06-14-18-11-56-915980 b/plugins/woocommerce/changelog/2024-06-14-18-11-56-915980 new file mode 100644 index 00000000000..dfd4e785f0f --- /dev/null +++ b/plugins/woocommerce/changelog/2024-06-14-18-11-56-915980 @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Added 'woocommerce_restore_order_item_stock' filter for restored line item stock on canceled orders diff --git a/plugins/woocommerce/includes/wc-stock-functions.php b/plugins/woocommerce/includes/wc-stock-functions.php index 42f79bd04b0..c023e0e4e9b 100644 --- a/plugins/woocommerce/includes/wc-stock-functions.php +++ b/plugins/woocommerce/includes/wc-stock-functions.php @@ -296,6 +296,7 @@ function wc_increase_stock_levels( $order_id ) { $item_name = $product->get_formatted_name(); $new_stock = wc_update_product_stock( $product, $item_stock_reduced, 'increase' ); + $old_stock = $new_stock - $item_stock_reduced; if ( is_wp_error( $new_stock ) ) { /* translators: %s item name. */ @@ -306,7 +307,18 @@ function wc_increase_stock_levels( $order_id ) { $item->delete_meta_data( '_reduced_stock' ); $item->save(); - $changes[] = $item_name . ' ' . ( $new_stock - $item_stock_reduced ) . '→' . $new_stock; + $changes[] = $item_name . ' ' . $old_stock . '→' . $new_stock; + + /** + * Fires when stock restored to a specific line item + * + * @since 9.1.0 + * @param WC_Order_Item_Product $item Order item data. + * @param int $new_stock New stock. + * @param int $old_stock Old stock. + * @param WC_Order $order Order data. + */ + do_action( 'woocommerce_restore_order_item_stock', $item, $new_stock, $old_stock, $order ); } if ( $changes ) {