Commit 9c5c22c9f04 for woocommerce

commit 9c5c22c9f04b452818c7475774a05de874b147a5
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Mon Nov 23 17:02:25 2026 +0100

    Remove applied coupons from carts when coupon codes are disabled (#71211)

    (cherry picked from commit ec911ef06bee868112b23098af39f8cc66d749e1)

    Co-authored-by: Jan Lysý <jan.lysy@automattic.com>

diff --git a/plugins/woocommerce/includes/class-wc-discounts.php b/plugins/woocommerce/includes/class-wc-discounts.php
index 6e6925d5722..b6f3a15af81 100644
--- a/plugins/woocommerce/includes/class-wc-discounts.php
+++ b/plugins/woocommerce/includes/class-wc-discounts.php
@@ -591,6 +591,25 @@ class WC_Discounts {
 		return $total_discount;
 	}

+	/**
+	 * Ensure coupons are enabled for the store when validating a cart, or throw exception.
+	 *
+	 * Coupons applied to a cart before coupons were disabled are kept in the session, so they are rejected here.
+	 * A `woocommerce_coupons_enabled` callback that returns true keeps them valid, as on the apply paths. One that
+	 * returns false while the setting is on (e.g. to hide the coupon form on a page) doesn't remove applied coupons.
+	 *
+	 * @since  11.4.0
+	 * @throws Exception Error message.
+	 * @return bool
+	 */
+	protected function validate_coupons_enabled() {
+		if ( $this->object instanceof WC_Cart && 'yes' !== get_option( 'woocommerce_enable_coupons' ) && ! wc_coupons_enabled() ) {
+			throw new Exception( esc_html__( 'Coupons are disabled.', 'woocommerce' ), WC_Coupon::E_WC_COUPON_INVALID_FILTERED ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
+		}
+
+		return true;
+	}
+
 	/**
 	 * Ensure coupon exists or throw exception.
 	 *
@@ -1245,6 +1264,7 @@ class WC_Discounts {
 	 */
 	public function is_coupon_valid( $coupon ) {
 		try {
+			$this->validate_coupons_enabled();
 			$this->validate_coupon_exists( $coupon );
 			$this->validate_coupon_usage_limit( $coupon );
 			$this->validate_coupon_user_usage_limit( $coupon );
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-discounts-tests.php b/plugins/woocommerce/tests/php/includes/class-wc-discounts-tests.php
index 036fa923d43..1d3e479a084 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-discounts-tests.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-discounts-tests.php
@@ -126,6 +126,92 @@ class WC_Discounts_Tests extends WC_Unit_Test_Case {
 		$this->assertEquals( 'invalid_coupon', $result->get_error_code(), 'We receive an appropriate WP_Error.' );
 	}

+	/**
+	 * Add a product to the cart and apply a 50% coupon while coupons are enabled.
+	 *
+	 * @return WC_Coupon The applied coupon.
+	 */
+	private function apply_percent_coupon_to_cart(): WC_Coupon {
+		update_option( 'woocommerce_enable_coupons', 'yes' );
+		WC()->cart->empty_cart();
+
+		$product = WC_Helper_Product::create_simple_product();
+		$product->set_regular_price( 10 );
+		$product->set_virtual( true );
+		$product->save();
+		WC()->cart->add_to_cart( $product->get_id(), 1 );
+
+		$coupon = WC_Helper_Coupon::create_coupon(
+			'half' . wp_generate_password( 6, false, false ),
+			array(
+				'discount_type' => 'percent',
+				'coupon_amount' => '50',
+			)
+		);
+		$this->assertTrue( WC()->cart->apply_coupon( $coupon->get_code() ), 'The coupon applies while coupons are enabled.' );
+
+		return $coupon;
+	}
+
+	/**
+	 * @testdox is_coupon_valid rejects a coupon already applied to the cart once coupons are disabled.
+	 */
+	public function test_is_coupon_valid_rejects_applied_coupon_when_coupons_are_disabled(): void {
+		$coupon = $this->apply_percent_coupon_to_cart();
+
+		update_option( 'woocommerce_enable_coupons', 'no' );
+
+		$result = ( new WC_Discounts( WC()->cart ) )->is_coupon_valid( $coupon );
+		$this->assertWPError( $result, 'Coupons are disabled, so the applied coupon should be invalid.' );
+		$this->assertSame( 'Coupons are disabled.', $result->get_error_message(), 'The error should say coupons are disabled.' );
+	}
+
+	/**
+	 * @testdox Cart totals and cart checks drop an applied coupon once coupons are disabled.
+	 */
+	public function test_cart_drops_applied_coupon_when_coupons_are_disabled(): void {
+		$coupon = $this->apply_percent_coupon_to_cart();
+		WC()->cart->calculate_totals();
+		$this->assertEquals( 5, WC()->cart->get_discount_total(), 'The coupon discounts the cart while coupons are enabled.' );
+
+		update_option( 'woocommerce_enable_coupons', 'no' );
+		WC()->cart->calculate_totals();
+		$this->assertEquals( 0, WC()->cart->get_discount_total(), 'No discount should be applied once coupons are disabled.' );
+		$this->assertEquals( 10, WC()->cart->get_total( 'edit' ), 'The cart total should be the full price.' );
+
+		WC()->cart->check_cart_coupons();
+		$this->assertNotContains( $coupon->get_code(), WC()->cart->get_applied_coupons(), 'The coupon should be removed from the cart.' );
+	}
+
+	/**
+	 * @testdox is_coupon_valid keeps an applied coupon valid when the coupons filter overrides the setting.
+	 * @testWith ["no", true]
+	 *           ["yes", false]
+	 *
+	 * @param string $option_value  Value of the woocommerce_enable_coupons option.
+	 * @param bool   $filter_result Value returned from the woocommerce_coupons_enabled filter.
+	 */
+	public function test_is_coupon_valid_follows_coupons_enabled_filter( string $option_value, bool $filter_result ): void {
+		$coupon = $this->apply_percent_coupon_to_cart();
+
+		update_option( 'woocommerce_enable_coupons', $option_value );
+		add_filter( 'woocommerce_coupons_enabled', $filter_result ? '__return_true' : '__return_false' );
+
+		$this->assertTrue( ( new WC_Discounts( WC()->cart ) )->is_coupon_valid( $coupon ), 'The applied coupon should stay valid.' );
+	}
+
+	/**
+	 * @testdox is_coupon_valid for an order is unaffected by the coupons setting.
+	 */
+	public function test_is_coupon_valid_for_order_ignores_coupons_setting(): void {
+		$coupon = WC_Helper_Coupon::create_coupon( 'order' . wp_generate_password( 6, false, false ) );
+		$order  = WC_Helper_Order::create_order();
+
+		update_option( 'woocommerce_enable_coupons', 'no' );
+
+		$this->assertTrue( ( new WC_Discounts( $order ) )->is_coupon_valid( $coupon ), 'Order coupons are validated without the store-wide setting.' );
+	}
+
 	/**
 	 * @testdox is_coupon_valid rejects a coupon when the cart subtotal is below its minimum spend.
 	 */