This commit is contained in:
commit
5417750757
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: fix
|
||||
|
||||
Decrease product total sales when an order is reversed
|
|
@ -872,22 +872,39 @@ function wc_order_search( $term ) {
|
|||
function wc_update_total_sales_counts( $order_id ) {
|
||||
$order = wc_get_order( $order_id );
|
||||
|
||||
if ( ! $order || $order->get_data_store()->get_recorded_sales( $order ) ) {
|
||||
if ( ! $order ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$recorded_sales = $order->get_data_store()->get_recorded_sales( $order );
|
||||
$reflected_order = in_array( $order->get_status(), array( 'cancelled', 'trash' ), true );
|
||||
|
||||
if ( ! $reflected_order && 'before_delete_post' === current_action() ) {
|
||||
$reflected_order = true;
|
||||
}
|
||||
|
||||
if ( $recorded_sales xor $reflected_order ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$operation = $recorded_sales && $reflected_order ? 'decrease' : 'increase';
|
||||
|
||||
if ( count( $order->get_items() ) > 0 ) {
|
||||
foreach ( $order->get_items() as $item ) {
|
||||
$product_id = $item->get_product_id();
|
||||
|
||||
if ( $product_id ) {
|
||||
$data_store = WC_Data_Store::load( 'product' );
|
||||
$data_store->update_product_sales( $product_id, absint( $item->get_quantity() ), 'increase' );
|
||||
$data_store->update_product_sales( $product_id, absint( $item->get_quantity() ), $operation );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( 'decrease' === $operation ) {
|
||||
$order->get_data_store()->set_recorded_sales( $order, false );
|
||||
} else {
|
||||
$order->get_data_store()->set_recorded_sales( $order, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when sales for an order are recorded
|
||||
|
@ -899,6 +916,14 @@ function wc_update_total_sales_counts( $order_id ) {
|
|||
add_action( 'woocommerce_order_status_completed', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_processing', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_on-hold', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_completed_to_cancelled', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_processing_to_cancelled', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_on-hold_to_cancelled', 'wc_update_total_sales_counts' );
|
||||
add_action( 'trashed_post', 'wc_update_total_sales_counts' );
|
||||
add_action( 'untrashed_post', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_trash_order', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_untrash_order', 'wc_update_total_sales_counts' );
|
||||
add_action( 'before_delete_post', 'wc_update_total_sales_counts' );
|
||||
|
||||
/**
|
||||
* Update used coupon amount for each coupon within an order.
|
||||
|
|
|
@ -56,4 +56,58 @@ class WC_Order_Functions_Test extends \WC_Unit_Test_Case {
|
|||
$this->assertEquals( 0, (int) $line_item->get_meta( '_reduced_stock', true ) );
|
||||
$this->assertEquals( 2, (int) $line_item->get_meta( '_restock_refunded_items', true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update_total_sales_counts and check total_sales after order reflection.
|
||||
*
|
||||
* Tests the fix for issue #23796
|
||||
*/
|
||||
public function test_wc_update_total_sales_counts() {
|
||||
|
||||
$product_id = WC_Helper_Product::create_simple_product()->get_id();
|
||||
|
||||
WC()->cart->add_to_cart( $product_id );
|
||||
|
||||
$order_id = WC_Checkout::instance()->create_order(
|
||||
array(
|
||||
'billing_email' => 'a@b.com',
|
||||
'payment_method' => 'dummy',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEquals( 0, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
$order = new WC_Order( $order_id );
|
||||
|
||||
$order->update_status( 'processing' );
|
||||
$this->assertEquals( 1, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
$order->update_status( 'cancelled' );
|
||||
$this->assertEquals( 0, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
$order->update_status( 'processing' );
|
||||
$this->assertEquals( 1, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
$order->update_status( 'completed' );
|
||||
$this->assertEquals( 1, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
$order->update_status( 'refunded' );
|
||||
$this->assertEquals( 1, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
$order->update_status( 'processing' );
|
||||
$this->assertEquals( 1, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
// Test trashing the order.
|
||||
$order->delete( false );
|
||||
$this->assertEquals( 0, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
// To successfully untrash, we need to grab a new instance of the order.
|
||||
wc_get_order( $order_id )->untrash();
|
||||
$this->assertEquals( 1, wc_get_product( $product_id )->get_total_sales() );
|
||||
|
||||
// Test full deletion of the order (again, we need to grab a new instance of the order).
|
||||
wc_get_order( $order_id )->delete( true );
|
||||
$this->assertEquals( 0, wc_get_product( $product_id )->get_total_sales() );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue