Automattic\WooCommerce\Internal\Admin\Settings\PaymentProviders

WooPayments::has_orders()privateWC 1.0

Check if the store has any paid orders.

Currently, we look at the past 90 days and only consider orders with status wc-completed, wc-processing, or wc-refunded.

Method of the class: WooPayments{}

No Hooks.

Return

true|false. Whether the store has any paid orders.

Usage

// private - for code of main (parent) class only
$result = $this->has_orders(): bool;

WooPayments::has_orders() code WC 9.6.1

private function has_orders(): bool {
	$store_has_orders_transient_name = self::PREFIX . 'store_has_orders';

	// First, get the stored value, if it exists.
	// This way we avoid costly DB queries and API calls.
	$has_orders = get_transient( $store_has_orders_transient_name );
	if ( false !== $has_orders ) {
		return filter_var( $has_orders, FILTER_VALIDATE_BOOLEAN );
	}

	// We need to determine the value.
	// Start with the assumption that the store doesn't have orders in the timeframe we look at.
	$has_orders = false;
	// By default, we will check for new orders every 6 hours.
	$expiration = 6 * HOUR_IN_SECONDS;

	// Get the latest completed, processing, or refunded order.
	$latest_order = wc_get_orders(
		array(
			'status'  => array( OrderInternalStatus::COMPLETED, OrderInternalStatus::PROCESSING, OrderInternalStatus::REFUNDED ),
			'limit'   => 1,
			'orderby' => 'date',
			'order'   => 'DESC',
		)
	);
	if ( ! empty( $latest_order ) ) {
		$latest_order = reset( $latest_order );
		// If the latest order is within the timeframe we look at, we consider the store to have orders.
		// Otherwise, it clearly doesn't have orders.
		if ( $latest_order instanceof WC_Abstract_Order
			&& strtotime( (string) $latest_order->get_date_created() ) >= strtotime( '-90 days' ) ) {

			$has_orders = true;

			// For ultimate efficiency, we will check again after 90 days from the latest order
			// because in all that time we will consider the store to have orders regardless of new orders.
			$expiration = strtotime( (string) $latest_order->get_date_created() ) + 90 * DAY_IN_SECONDS - time();
		}
	}

	// Store the value for future use.
	set_transient( $store_has_orders_transient_name, $has_orders ? 'yes' : 'no', $expiration );

	return $has_orders;
}