Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks

Products::has_productspublic staticWC 1.0

Check if the store has any user created published products.

Method of the class: Products{}

No Hooks.

Returns

true|false.

Usage

$result = Products::has_products();

Products::has_products() code WC 10.6.2

public static function has_products() {
	$product_exists = get_transient( self::HAS_PRODUCT_TRANSIENT );
	if ( $product_exists ) {
		return 'yes' === $product_exists;
	}

	global $wpdb;

	/*
	 * Check if any valid products exist and return 'yes' or 'no'
	 * A valid product must:
	 * 1. Be a published product post type
	 * 2. Meet one of these conditions:
	 *    - Have been edited by a user (_edit_last meta exists), OR
	 *    - Not have _headstart_post meta, OR
	 *    - Have _headstart_post meta but it's NULL
	 */
	$value = $wpdb->get_var(
		$wpdb->prepare(
			"SELECT IF(
				EXISTS (
					SELECT 1 FROM {$wpdb->posts} p
					WHERE p.post_type = %s
					AND p.post_status = %s
					AND (
						EXISTS (
							SELECT 1 FROM {$wpdb->postmeta} pm
							WHERE pm.post_id = p.ID
							AND pm.meta_key = %s
						)
						OR
						NOT EXISTS (
							SELECT 1 FROM {$wpdb->postmeta} pm
							WHERE pm.post_id = p.ID
							AND pm.meta_key = %s
						)
						OR
						EXISTS (
							SELECT 1 FROM {$wpdb->postmeta} pm
							WHERE pm.post_id = p.ID
							AND pm.meta_key = %s
							AND pm.meta_value = ''
						)
					)
					LIMIT 1
				),
				'yes', 'no'
			)",
			'product',
			ProductStatus::PUBLISH,
			'_edit_last',
			'_headstart_post',
			'_headstart_post'
		)
	);

	set_transient( self::HAS_PRODUCT_TRANSIENT, $value );
	return 'yes' === $value;
}