Get Current WooCommerce Version Number in Plugin

The Function wpbo_get_woo_version_number()

As WooCommerce releases updates, your plugin may need to function differently based on which version of WooCommerce your user is using. The function below will grab the current version number of WooCommerce or return null if the plugin is not installed.


function wpbo_get_woo_version_number() {
        // If get_plugins() isn't available, require it
	if ( ! function_exists( 'get_plugins' ) )
		require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	
        // Create the plugins folder and file variables
	$plugin_folder = get_plugins( '/' . 'woocommerce' );
	$plugin_file = 'woocommerce.php';
	
	// If the plugin version number is set, return it 
	if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) {
		return $plugin_folder[$plugin_file]['Version'];

	} else {
	// Otherwise return null
		return NULL;
	}
}

Use Case

Say your plugin creates an error notices, a ability that has been restructured in WooCommerce version 2.1.

Error Statement Pre 2.1


$woocommerce->add_error( sprintf( __( "This is my custom error", "your-theme-language" ) );

Error Statement Version 2.1


wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ), 'error' );

Use the error statement that works for your user



$woo_version = wpbo_get_woo_version_number();

if ( $woo_version >= 2.1 ) {
     wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ), 'error' );
} else if ( $woo_version < 2.1 ) {
     $woocommerce->add_error( sprintf( __( "This is my custom error", "your-theme-language" ) );
} else {
     // User does not have plugin installed
}

Performance Note

In production you should assign the version number to a global constant or class variable.