Automattic\WooCommerce\StoreApi\Utilities

OrderController::validate_coupon_global_usage_limitprotectedWC 1.0

Check the coupon's global usage limit against the order.

Skipped once the order has recorded its own usage, so it is not counted against itself.

Method of the class: OrderController{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_coupon_global_usage_limit( $coupon, $order ): void;
$coupon(WC_Coupon) (required)
Coupon object applied to the order.
$order(WC_Order) (required)
Order object.

OrderController::validate_coupon_global_usage_limit() code WC 11.0.1

protected function validate_coupon_global_usage_limit( \WC_Coupon $coupon, \WC_Order $order ): void {
	$usage_limit = $coupon->get_usage_limit();

	if ( ! $usage_limit || $order->get_recorded_coupon_usage_counts() ) {
		return;
	}

	// Include tentative holds, matching WC_Discounts::validate_coupon_usage_limit().
	$data_store      = $coupon->get_data_store();
	$tentative_usage = is_callable( array( $data_store, 'get_tentative_usage_count' ) )
		? (int) $data_store->get_tentative_usage_count( $coupon->get_id() )
		: 0;

	if ( $coupon->get_usage_count() + $tentative_usage >= $usage_limit ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
		throw new Exception( $coupon->get_coupon_error( \WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED ) );
	}
}