Automattic\WooCommerce\StoreApi\Utilities
OrderController::validate_coupon_usage_limit
Check usage restrictions of a coupon against the order.
Метод класса: OrderController{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->validate_coupon_usage_limit( $coupon, $order );
- $coupon(WC_Coupon) (обязательный)
- Coupon object applied to the cart.
- $order(WC_Order) (обязательный)
- Order object.
Код OrderController::validate_coupon_usage_limit() OrderController::validate coupon usage limit WC 10.4.3
protected function validate_coupon_usage_limit( \WC_Coupon $coupon, \WC_Order $order ) {
$coupon_usage_limit = $coupon->get_usage_limit_per_user();
if ( 0 === $coupon_usage_limit ) {
return;
}
// First, we check a logged in customer usage count, which happens against their user id, billing email, and account email.
if ( $order->get_customer_id() ) {
// We get usage per user id and associated emails.
$usage_count = $this->get_usage_per_aliases(
$coupon,
array(
$order->get_billing_email(),
$order->get_customer_id(),
$this->get_email_from_user_id( $order->get_customer_id() ),
)
);
} else {
// Otherwise we check if the email doesn't belong to an existing user.
// This will get us any user ids for the given billing email.
$user_ids = wc_get_container()->get( CustomerSearchService::class )->find_user_ids_by_billing_email_for_coupons_usage_lookup( array( $order->get_billing_email() ) );
// Convert all found user ids to a list of email addresses.
$user_emails = array_map( array( $this, 'get_email_from_user_id' ), $user_ids );
// This matches a user against the given billing email and gets their ID/email/billing email.
$found_user = get_user_by( 'email', $order->get_billing_email() );
if ( $found_user ) {
$user_ids[] = $found_user->ID;
$user_emails[] = $found_user->user_email;
$user_emails[] = get_user_meta( $found_user->ID, 'billing_email', true );
}
// Finally, grab usage count for all found IDs and emails.
$usage_count = $this->get_usage_per_aliases(
$coupon,
array_merge(
$user_emails,
$user_ids,
array( $order->get_billing_email() )
)
);
}
if ( $usage_count >= $coupon_usage_limit ) {
throw new Exception( $coupon->get_coupon_error( \WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED ) );
}
}