WC_Discounts::validate_coupon_allowed_emails
Ensure coupon is valid for allowed emails or throw exception.
Method of the class: WC_Discounts{}
No Hooks.
Returns
true|false.
Usage
// protected - for code of main (parent) or child class $result = $this->validate_coupon_allowed_emails( $coupon );
- $coupon(WC_Coupon) (required)
- Coupon data.
Changelog
| Since 8.6.0 | Introduced. |
WC_Discounts::validate_coupon_allowed_emails() WC Discounts::validate coupon allowed emails code WC 10.5.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;
}