WC_Discounts::validate_coupon_user_usage_limit( WC_Coupon $coupon, int $user_id )

Ensure coupon user usage limit is valid or throw exception.


Description Description

Per user usage limit – check here if user is logged in (against user IDs). Checked again for emails later on in WC_Cart::check_customer_coupons().


Parameters Parameters

$coupon

(Required) Coupon data.

$user_id

(Required) User ID.


Top ↑

Return Return

(bool)


Top ↑

Source Source

File: includes/class-wc-discounts.php

	protected function validate_coupon_user_usage_limit( $coupon, $user_id = 0 ) {
		if ( empty( $user_id ) ) {
			if ( $this->object instanceof WC_Order ) {
				$user_id = $this->object->get_customer_id();
			} else {
				$user_id = get_current_user_id();
			}
		}

		if ( $coupon && $user_id && apply_filters( 'woocommerce_coupon_validate_user_usage_limit', $coupon->get_usage_limit_per_user() > 0, $user_id, $coupon, $this ) && $coupon->get_id() && $coupon->get_data_store() ) {
			$data_store  = $coupon->get_data_store();
			$usage_count = $data_store->get_usage_by_user_id( $coupon, $user_id );
			if ( $usage_count >= $coupon->get_usage_limit_per_user() ) {
				throw new Exception( __( 'Coupon usage limit has been reached.', 'woocommerce' ), 106 );
			}
		}

		return true;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.2.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.