Automattic\WooCommerce\Admin\API\Reports\Orders\Stats

DataStore::has_fulfillment_status_columnpublic staticWC 1.0

Check if the wc_order_stats table has the fulfillment_status column.

Method of the class: DataStore{}

No Hooks.

Returns

true|false.

Usage

$result = DataStore::has_fulfillment_status_column();

DataStore::has_fulfillment_status_column() code WC 10.4.3

public static function has_fulfillment_status_column() {
	$column_status = get_option( self::OPTION_ORDER_STATS_TABLE_HAS_COLUMN_ORDER_FULFILLMENT_STATUS );

	if ( ! empty( $column_status ) ) {
		return 'yes' === $column_status;
	}

	global $wpdb;

	$table_name = self::get_db_table_name();

	// Check if the table exists.
	$table_exists = $wpdb->get_var(
		$wpdb->prepare(
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name cannot be prepared.
			'SHOW TABLES LIKE %s',
			$table_name
		)
	);

	// If table still does not exist, return false without setting the option to allow for table to be created with the column.
	if ( ! $table_exists ) {
		return false;
	}

	$column_exists = $wpdb->get_var(
		$wpdb->prepare(
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name cannot be prepared.
			"SHOW COLUMNS FROM `{$table_name}` LIKE %s",
			'fulfillment_status'
		)
	);

	if ( ! empty( $column_exists ) ) {
		update_option( self::OPTION_ORDER_STATS_TABLE_HAS_COLUMN_ORDER_FULFILLMENT_STATUS, 'yes', false );
		return true;
	}

	// Update the option to indicate that the column does not exist.
	update_option( self::OPTION_ORDER_STATS_TABLE_HAS_COLUMN_ORDER_FULFILLMENT_STATUS, 'no', false );
	return false;
}