WC_Discounts::validate_coupon_allowed_emails()protectedWC 8.6.0

Ensure coupon is valid for allowed emails or throw exception.

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

Хуков нет.

Возвращает

true|false.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->validate_coupon_allowed_emails( $coupon );
$coupon(WC_Coupon) (обязательный)
Coupon data.

Список изменений

С версии 8.6.0 Введена.

Код WC_Discounts::validate_coupon_allowed_emails() WC 9.6.0

protected function validate_coupon_allowed_emails( $coupon ) {

	$restrictions = $coupon->get_email_restrictions();

	if ( ! is_array( $restrictions ) || empty( $restrictions ) ) {
		return true;
	}

	$user         = wp_get_current_user();
	$check_emails = array( $user->user_email );

	if ( $this->object instanceof WC_Cart ) {
		$check_emails[] = $this->object->get_customer()->get_billing_email();
	} elseif ( $this->object instanceof WC_Order ) {
		$check_emails[] = $this->object->get_billing_email();
	}

	$check_emails = array_unique(
		array_filter(
			array_map(
				'strtolower',
				array_map(
					'sanitize_email',
					$check_emails
				)
			)
		)
	);

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

	return true;
}