Commit c3bcc02d428 for woocommerce
commit c3bcc02d428480603635b8d824942e8e399199b1
Author: Luigi Teschio <gigitux@gmail.com>
Date: Sat Nov 21 02:48:47 2026 +0100
Recalculate cart totals after removing all coupons in the Store API (#71217)
(cherry picked from commit f31b6036b57da59e5ab795876a206b69087ca761)
Co-authored-by: Yuliyan Slavchev <yuliyan.slavchev@gmail.com>
diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/CartCoupons.php b/plugins/woocommerce/src/StoreApi/Routes/V1/CartCoupons.php
index e56bf4949c6..2daf1166f69 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/CartCoupons.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/CartCoupons.php
@@ -130,6 +130,9 @@ class CartCoupons extends AbstractCartRoute {
$cart->remove_coupons();
+ // Unlike remove_coupon(), remove_coupons() does not recalculate totals, and cart_updated() copies them to the draft order.
+ $cart->calculate_totals();
+
return new \WP_REST_Response( [], 200 );
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/CartCoupons.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/CartCoupons.php
index 29b58d53966..f01b4dcfb16 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/CartCoupons.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/CartCoupons.php
@@ -8,6 +8,7 @@ namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Routes;
use Automattic\WooCommerce\Tests\Blocks\StoreApi\Routes\ControllerTestCase;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
use Automattic\WooCommerce\Tests\Blocks\Helpers\ValidateSchema;
+use Automattic\WooCommerce\StoreApi\Utilities\OrderController;
/**
* Cart Coupons Controller Tests.
@@ -205,6 +206,55 @@ class CartCoupons extends ControllerTestCase {
);
}
+ /**
+ * @testdox Deleting all coupons should remove their discount from the cart totals and line totals.
+ */
+ public function test_delete_items_recalculates_totals(): void {
+ $this->coupon->set_amount( 5 );
+ $this->coupon->save();
+ wc()->cart->calculate_totals();
+
+ $this->assertEquals( 5, wc()->cart->get_discount_total() );
+
+ $request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/coupons' );
+ $request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertSame( 200, $response->get_status() );
+ $this->assertEmpty( wc()->cart->get_applied_coupons() );
+ $this->assertEquals( 0, wc()->cart->get_discount_total() );
+ $this->assertEquals( 20, wc()->cart->get_total( 'edit' ) );
+
+ foreach ( wc()->cart->get_cart() as $cart_item ) {
+ $this->assertEquals( $cart_item['line_subtotal'], $cart_item['line_total'] );
+ }
+ }
+
+ /**
+ * @testdox Deleting all coupons should remove their discount from the draft order.
+ */
+ public function test_delete_items_removes_discount_from_draft_order(): void {
+ $this->coupon->set_amount( 5 );
+ $this->coupon->save();
+ wc()->cart->calculate_totals();
+
+ $draft_order = ( new OrderController() )->create_order_from_cart();
+ wc()->session->set( 'store_api_draft_order', $draft_order->get_id() );
+
+ $this->assertEquals( 15, $draft_order->get_total() );
+
+ $request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/coupons' );
+ $request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertSame( 200, $response->get_status() );
+
+ $draft_order = wc_get_order( $draft_order->get_id() );
+ $this->assertEmpty( $draft_order->get_coupon_codes() );
+ $this->assertEquals( 0, $draft_order->get_discount_total() );
+ $this->assertEquals( 20, $draft_order->get_total() );
+ }
+
/**
* Test conversion of cart item to rest response.
*/