Automattic\WooCommerce\StoreApi\Utilities

OrderController::get_usage_per_aliases()privateWC 1.0

Get the usage count for a coupon based on a list of aliases (ids, emails).

Метод класса: OrderController{}

Хуков нет.

Возвращает

Int.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->get_usage_per_aliases( $coupon, $aliases );
$coupon(\WC_Coupon) (обязательный)
Coupon object applied to the cart.
$aliases(массив) (обязательный)
List of aliases to check.

Код OrderController::get_usage_per_aliases() WC 9.6.1

private function get_usage_per_aliases( $coupon, $aliases ) {
	global $wpdb;
	$aliases        = array_unique( array_filter( $aliases ) );
	$aliases_string = "('" . implode( "','", array_map( 'esc_sql', $aliases ) ) . "')";
	$usage_count    = $wpdb->get_var(
		$wpdb->prepare(
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			"SELECT COUNT( meta_id ) FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_used_by' AND meta_value IN {$aliases_string};",
			$coupon->get_id(),
		)
	);

	$data_store = $coupon->get_data_store();
	// Coupons can be held for an x amount of time before being applied to an order, so we need to check if it's already being held in (maybe via another flow).
	$tentative_usage_count = $data_store->get_tentative_usages_for_user( $coupon->get_id(), $aliases );
	return $tentative_usage_count + $usage_count;
}