From 92f94248cd3e7e01e4c49738acd35a8beeccd785 Mon Sep 17 00:00:00 2001 From: Yousuf Hossain Date: Thu, 9 Mar 2023 03:05:40 +0600 Subject: [PATCH] Added woocommerce_reduce_order_item_stock action hook to let other plugin hook functionalities without looping through the order items again and again. (#34721) * Added woocommerce_reduce_order_item_stock action hook to let other plugins do somthing without looping again the order line items * changed "product data" to "change details" and updated docblock as instructed * maintained WP coding standards and change @since docblock tag --- .../changelog/woocommerce-reduce-order-item-stock | 4 ++++ plugins/woocommerce/includes/wc-stock-functions.php | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/woocommerce-reduce-order-item-stock diff --git a/plugins/woocommerce/changelog/woocommerce-reduce-order-item-stock b/plugins/woocommerce/changelog/woocommerce-reduce-order-item-stock new file mode 100644 index 00000000000..5c46be6f864 --- /dev/null +++ b/plugins/woocommerce/changelog/woocommerce-reduce-order-item-stock @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +Added woocommerce_reduce_order_item_stock action hook diff --git a/plugins/woocommerce/includes/wc-stock-functions.php b/plugins/woocommerce/includes/wc-stock-functions.php index 4a836dd07df..5adf39c178a 100644 --- a/plugins/woocommerce/includes/wc-stock-functions.php +++ b/plugins/woocommerce/includes/wc-stock-functions.php @@ -197,11 +197,22 @@ function wc_reduce_stock_levels( $order_id ) { $item->add_meta_data( '_reduced_stock', $qty, true ); $item->save(); - $changes[] = array( + $change = array( 'product' => $product, 'from' => $new_stock + $qty, 'to' => $new_stock, ); + $changes[] = $change; + + /** + * Fires when stock reduced to a specific line item + * + * @param WC_Order_Item_Product $item Order item data. + * @param array $change Change Details. + * @param WC_Order $order Order data. + * @since 7.6.0 + */ + do_action( 'woocommerce_reduce_order_item_stock', $item, $change, $order ); } wc_trigger_stock_change_notifications( $order, $changes );