Automattic\WooCommerce\StoreApi\Utilities

OrderController::validate_coupon_email_restrictionprotectedWC 1.0

Check email restrictions of a coupon against the order.

Метод класса: OrderController{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->validate_coupon_email_restriction( $coupon, $order );
$coupon(WC_Coupon) (обязательный)
Coupon object applied to the cart.
$order(WC_Order) (обязательный)
Order object.

Код OrderController::validate_coupon_email_restriction() WC 10.5.0

protected function validate_coupon_email_restriction( \WC_Coupon $coupon, \WC_Order $order ) {
	$restrictions = $coupon->get_email_restrictions();

	if ( empty( $restrictions ) ) {
		return;
	}

	$check_emails = array();

	// Check the logged-in user's email.
	$current_user = wp_get_current_user();
	if ( $current_user->exists() ) {
		$user_email = trim( sanitize_email( $current_user->user_email ) );
		if ( ! empty( $user_email ) ) {
			$check_emails[] = strtolower( $user_email );
		}
	}

	// Also check the billing email from the order.
	$billing_email = $order->get_billing_email();
	if ( ! empty( $billing_email ) ) {
		$billing_email = trim( sanitize_email( $billing_email ) );
		if ( ! empty( $billing_email ) ) {
			$check_emails[] = strtolower( $billing_email );
		}
	}

	// Remove duplicates and empty values.
	$check_emails = array_unique( array_filter( $check_emails ) );

	if ( ! empty( $check_emails ) && ! DiscountsUtil::is_coupon_emails_allowed( $check_emails, $restrictions ) ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
		throw new Exception( $coupon->get_coupon_error( \WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED ) );
	}
}