Skip to content

Instantly share code, notes, and snippets.

@hlashbrooke
Last active December 13, 2023 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hlashbrooke/9133402 to your computer and use it in GitHub Desktop.
Save hlashbrooke/9133402 to your computer and use it in GitHub Desktop.
WooCommerce - Check if you are running a specified WooCommerce version (or higher)
<?php
function woocommerce_version_check( $version = '2.1' ) {
if ( function_exists( 'is_woocommerce_active' ) && is_woocommerce_active() ) {
global $woocommerce;
if( version_compare( $woocommerce->version, $version, ">=" ) ) {
return true;
}
}
return false;
}
?>
<?php
if( woocommerce_version_check() ) {
// Use new, updated functions
} else {
// Use older, deprecated functions
}
?>
@pablo-sg-pacheco
Copy link

I think this function is_woocommerce_active() no longer exists. So i prefer to check if class exists, like this:

public static function version_check( $version = '3.0' ) {
	if ( class_exists( 'WooCommerce' ) ) {
		global $woocommerce;
		if ( version_compare( $woocommerce->version, $version, ">=" ) ) {
			return true;
		}
	}
	return false;
}

@passatgt
Copy link

<?php echo WC()->version; ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment