WC_Abstract_Order::remove_coupon │ public │ WC 3.2.0

Remove a coupon from the order and recalculate totals.

Coupons affect line item totals, but there is no relationship between coupon and line total, so to remove a coupon we need to work from the line subtotal (price before discount) and re-apply all coupons in this order.

Manual discounts are not affected; those are separate and do not affect stored line totals.

Method of the class: WC_Abstract_Order{}

Hooks from the method

Returns

bool. TRUE if coupon was removed, FALSE otherwise.

Usage

$WC_Abstract_Order = new WC_Abstract_Order();
$WC_Abstract_Order->remove_coupon( $code );
$code(string) (required)
Coupon code.

Changelog

Since 3.2.0 Introduced.
Since 7.6.0 Returns a boolean indicating success.

WC_Abstract_Order::remove_coupon() code WC 11.2.1

public function remove_coupon( $code ) {
	$coupons = $this->get_items( 'coupon' );

	// Remove the coupon line.
	foreach ( $coupons as $item_id => $coupon ) {
		if ( wc_is_same_coupon( $coupon->get_code(), $code ) ) {
			$this->remove_item( $item_id );
			$coupon_object = new WC_Coupon( $code );
			$coupon_object->decrease_usage_count( $this->get_user_id() );
			$this->recalculate_coupons();

			/**
			 * Action hook fired when a coupon is removed from an order.
			 *
			 * @param  WC_Coupon $coupon_object The removed coupon object.
			 * @param  WC_Order  $order         The current order object.
			 *
			 * @since 10.8.0
			 */
			do_action( 'woocommerce_order_removed_coupon', $coupon_object, $this );

			return true;
		}
	}

	return false;
}