From 7105a7a17e64d56387571223aa1874be8942f349 Mon Sep 17 00:00:00 2001 From: roykho Date: Fri, 23 Apr 2021 12:14:51 -0700 Subject: [PATCH] Update restock logic during refunds closes #29502 --- .../class-wc-admin-list-table-orders.php | 1 + .../meta-boxes/views/html-order-item-meta.php | 1 + includes/admin/wc-admin-functions.php | 14 +++++++------- includes/wc-order-functions.php | 4 ++++ 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/includes/admin/list-tables/class-wc-admin-list-table-orders.php b/includes/admin/list-tables/class-wc-admin-list-table-orders.php index b2a0e511886..417f2819f1a 100644 --- a/includes/admin/list-tables/class-wc-admin-list-table-orders.php +++ b/includes/admin/list-tables/class-wc-admin-list-table-orders.php @@ -454,6 +454,7 @@ class WC_Admin_List_Table_Orders extends WC_Admin_List_Table { 'method_id', 'cost', '_reduced_stock', + '_restock_refunded_items', ) ); diff --git a/includes/admin/meta-boxes/views/html-order-item-meta.php b/includes/admin/meta-boxes/views/html-order-item-meta.php index ceb33b450a5..9056a957668 100644 --- a/includes/admin/meta-boxes/views/html-order-item-meta.php +++ b/includes/admin/meta-boxes/views/html-order-item-meta.php @@ -17,6 +17,7 @@ $hidden_order_itemmeta = apply_filters( 'method_id', 'cost', '_reduced_stock', + '_restock_refunded_items', ) ); ?>
diff --git a/includes/admin/wc-admin-functions.php b/includes/admin/wc-admin-functions.php index 3131dd9ea05..3f9eb051285 100644 --- a/includes/admin/wc-admin-functions.php +++ b/includes/admin/wc-admin-functions.php @@ -206,17 +206,17 @@ function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) { return false; } - $product = $item->get_product(); - $item_quantity = wc_stock_amount( $item_quantity >= 0 ? $item_quantity : $item->get_quantity() ); - $already_reduced_stock = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) ); + $product = $item->get_product(); + $item_quantity = wc_stock_amount( $item_quantity >= 0 ? $item_quantity : $item->get_quantity() ); + $already_reduced_stock = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) ); + $restock_refunded_items = wc_stock_amount( $item->get_meta( '_restock_refunded_items', true ) ); if ( ! $product || ! $product->managing_stock() ) { return false; } - $order = $item->get_order(); - $refunded_item_quantity = $order->get_qty_refunded_for_item( $item->get_id() ); - $diff = $item_quantity + $refunded_item_quantity - $already_reduced_stock; + $order = $item->get_order(); + $diff = $item_quantity - $restock_refunded_items - $already_reduced_stock; /* * 0 as $item_quantity usually indicates we're deleting the order item. @@ -238,7 +238,7 @@ function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) { return $new_stock; } - $item->update_meta_data( '_reduced_stock', $item_quantity + $refunded_item_quantity ); + $item->update_meta_data( '_reduced_stock', $item_quantity - $restock_refunded_items ); $item->save(); if ( $item_quantity > 0 ) { diff --git a/includes/wc-order-functions.php b/includes/wc-order-functions.php index a17bae8244a..e8c905493f2 100644 --- a/includes/wc-order-functions.php +++ b/includes/wc-order-functions.php @@ -715,9 +715,13 @@ function wc_restock_refunded_items( $order, $refunded_line_items ) { $item_stock_reduced = $item_stock_reduced - $qty_to_refund; if ( 0 < $item_stock_reduced ) { + // Keeps track of total running tally of reduced stock. $item->update_meta_data( '_reduced_stock', $item_stock_reduced ); + // Keeps track of only refunded items that needs restock. + $item->update_meta_data( '_restock_refunded_items', $qty_to_refund ); } else { $item->delete_meta_data( '_reduced_stock' ); + $item->delete_meta_data( '_restock_refunded_items' ); } /* translators: 1: product ID 2: old stock level 3: new stock level */