Automattic\WooCommerce\Internal\Admin\Suggestions\Incentives

WooPayments::has_wcpay │ private │ WC 1.0

Check if the WooPayments payment gateway is active and set up or was at some point, or there are live-mode orders processed with it, at some moment.

Method of the class: WooPayments{}

No Hooks.

Returns

boolean. Whether the store has WooPayments.

Usage

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

WooPayments::has_wcpay() code WC 11.1.2

private function has_wcpay(): bool {
	// First, get the stored value, if it exists.
	// This way we avoid costly DB queries and API calls.
	// Basically, we only want to know if WooPayments was in use in the past.
	// Since the past can't be changed, neither can this value.
	$had_wcpay = get_option( $this->store_had_woopayments_option_name );
	if ( false !== $had_wcpay ) {
		$stored_value = filter_var( $had_wcpay, FILTER_VALIDATE_BOOLEAN );

		// A cached 'no' holds whatever version produced it, since each revision of this logic
		// only narrows what qualifies: a store that didn't qualify before can't start now.
		//
		// A cached 'yes' is trusted only when the current version produced it. Earlier
		// revisions counted test-mode usage - a test-drive account, a test-mode order - as
		// the real thing and froze that, so those get re-derived once. The WooPayments
		// plugin writes this same option from its own copy of this logic, which may still
		// be an older one, so this is what lets the two ship in either order instead of
		// whichever runs first winning permanently.
		if ( ! $stored_value
			|| (int) get_option( $this->store_had_woopayments_version_option_name, 0 ) >= self::STORE_HAD_WOOPAYMENTS_LOGIC_VERSION ) {

			return $stored_value;
		}
	}

	// We need to determine the value.
	// Start with the assumption that the store didn't have WooPayments in use.
	$had_wcpay = false;

	// We consider the store to have WooPayments if there is meaningful live account data
	// in the WooPayments account cache.
	// This implies that WooPayments was active at some point and that it was connected with a live account.
	if ( $this->has_wcpay_account_data() ) {
		$had_wcpay = true;
	}

	// If there is at least one live-mode order processed with WooPayments, we consider the store to have WooPayments.
	// Test-mode orders (e.g. placed while trialing WooPayments with a test-drive account) don't count
	// since they don't represent real usage of WooPayments.
	//
	// Orders carrying no order mode meta at all don't count either, since the meta is what tells the two
	// apart. That covers orders placed before WooPayments started saving it (April 2022, WooPayments 4.1.0)
	// and orders created outside the checkout flow that saves it, like WooPayments Subscriptions renewals.
	// Such a store may end up considered incentive-eligible despite having used WooPayments, which is the
	// far less harmful direction to err in than permanently withholding incentives from a store that only
	// ever tried WooPayments out.
	if ( false === $had_wcpay && ! empty(
		wc_get_orders(
			array(
				'payment_method' => 'woocommerce_payments',
				'return'         => 'ids',
				'limit'          => 1,
				'orderby'        => 'none',
				// Use the order mode meta saved by WooPayments to only count live-mode orders.
				// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
				'meta_key'       => '_wcpay_mode',
				// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
				'meta_value'     => 'prod',
			)
		)
	) ) {
		$had_wcpay = true;
	}

	// Store the value, and the logic version that determined it, for future use.
	update_option( $this->store_had_woopayments_option_name, $had_wcpay ? 'yes' : 'no' );
	update_option( $this->store_had_woopayments_version_option_name, self::STORE_HAD_WOOPAYMENTS_LOGIC_VERSION );

	return $had_wcpay;
}