WC_Admin_Marketplace_Promotions::filter_out_inactive_promotions()private staticWC 1.0

Find promotions that are still active – they have a date range that includes the current date.

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

Хуков нет.

Возвращает

Массив.

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

$result = WC_Admin_Marketplace_Promotions::filter_out_inactive_promotions( $promotions );
$promotions(?array)
Data about current promotions.
По умолчанию: array()

Код WC_Admin_Marketplace_Promotions::filter_out_inactive_promotions() WC 9.4.2

private static function filter_out_inactive_promotions( $promotions = array() ) {
	$now_date_time     = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
	$active_promotions = array();

	foreach ( $promotions as $promotion ) {
		if ( ! isset( $promotion['date_from_gmt'] ) || ! isset( $promotion['date_to_gmt'] ) ) {
			continue;
		}

		try {
			$date_from_gmt = new DateTime( $promotion['date_from_gmt'], new DateTimeZone( 'UTC' ) );
			$date_to_gmt   = new DateTime( $promotion['date_to_gmt'], new DateTimeZone( 'UTC' ) );
		} catch ( \Exception $ex ) {
			continue;
		}

		if ( $now_date_time >= $date_from_gmt && $now_date_time <= $date_to_gmt ) {
			$active_promotions[] = $promotion;
		}
	}

	// Sort promotions so the ones starting more recently are at the top.
	usort(
		$active_promotions,
		function ( $a, $b ) {
			return $b['date_from_gmt'] <=> $a['date_from_gmt'];
		}
	);

	return $active_promotions;
}