WC_Coupon_Data_Store_CPT::get_usage_by_user_id()publicWC 3.0.0

Get the number of uses for a coupon by user ID.

Method of the class: WC_Coupon_Data_Store_CPT{}

No Hooks.

Return

Int.

Usage

$WC_Coupon_Data_Store_CPT = new WC_Coupon_Data_Store_CPT();
$WC_Coupon_Data_Store_CPT->get_usage_by_user_id( $coupon, $user_id );
$coupon(WC_Coupon) (required) (passed by reference — &)
Coupon object.
$user_id(int) (required)
User ID.

Changelog

Since 3.0.0 Introduced.

WC_Coupon_Data_Store_CPT::get_usage_by_user_id() code WC 9.4.2

public function get_usage_by_user_id( &$coupon, $user_id ) {
	global $wpdb;
	$usage_count           = $wpdb->get_var(
		$wpdb->prepare(
			"SELECT COUNT( meta_id ) FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_used_by' AND meta_value = %s;",
			$coupon->get_id(),
			$user_id
		)
	);
	$tentative_usage_count = $this->get_tentative_usages_for_user( $coupon->get_id(), array( $user_id ) );
	return $tentative_usage_count + $usage_count;
}