WC_Coupon_Data_Store_CPT::get_tentative_usage_query_for_user()privateWC 1.0

Generate query to calculate tentative usages for the coupon by the user.

Method of the class: WC_Coupon_Data_Store_CPT{}

No Hooks.

Return

String. Tentative usages query.

Usage

// private - for code of main (parent) class only
$result = $this->get_tentative_usage_query_for_user( $coupon_id, $user_aliases );
$coupon_id(int) (required)
Coupon ID.
$user_aliases(array) (required)
List of user aliases to check for usages.

WC_Coupon_Data_Store_CPT::get_tentative_usage_query_for_user() code WC 8.7.0

private function get_tentative_usage_query_for_user( $coupon_id, $user_aliases ) {
	global $wpdb;

	$format = implode( "','", array_fill( 0, count( $user_aliases ), '%s' ) );

	// Note that if you are debugging, `_maybe_used_by_%` will be converted to `_maybe_used_by_{...very long str...}` to very long string. This is expected, and is automatically corrected while running the insert query.
	return $wpdb->prepare(
		"
			SELECT COUNT( meta_id ) FROM $wpdb->postmeta
			WHERE {$wpdb->postmeta}.meta_key like %s
			AND {$wpdb->postmeta}.meta_key > %s
			AND {$wpdb->postmeta}.post_id = %d
			AND {$wpdb->postmeta}.meta_value IN ('$format')
			FOR UPDATE
			",
		array_merge(
			array(
				'_maybe_used_by_%',
				'_maybe_used_by_' . time(),
				$coupon_id,
			),
			$user_aliases
		)
	); // WPCS: unprepared SQL ok.
}