Automattic\WooCommerce\Internal\Admin
WcPayWelcomePage::has_orders()
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: WcPayWelcomePage{}
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;
WcPayWelcomePage::has_orders() WcPayWelcomePage::has orders code WC 9.3.3
private function has_orders(): bool { // First, get the stored value, if it exists. // This way we avoid costly DB queries and API calls. $has_orders = get_transient( self::HAS_ORDERS_TRANSIENT_NAME ); if ( false !== $has_orders ) { return 'yes' === $has_orders; } // 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( 'wc-completed', 'wc-processing', 'wc-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( $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( $latest_order->get_date_created() ) + 90 * DAY_IN_SECONDS - time(); } } // Store the value for future use. set_transient( self::HAS_ORDERS_TRANSIENT_NAME, $has_orders ? 'yes' : 'no', $expiration ); return $has_orders; }