I want to show an admin notice which should show if the WooCommerce is not at the latest version. I tried with the following function which does not work. Any help will be appreciated.
function check_wc_version($version = '2.6.1'){
if ( function_exists( 'is_woocommerce_active' ) && is_woocommerce_active() ) {
global $woocommerce;
if( version_compare( $woocommerce, $version, ">=" ) ) {
echo 'Show some notice here';
}
}
}
add_action('admin_notices', 'check_wc_version');
Well your function is on the right track, but doesn’t work because the
$woocommerce
isn’t a version number. It’s a global variable that has been deprecated but holds the instance of the mainWooCommerce
class. You can now get the singleton instance of that class via the functionWC()
. And one of the class variables isversion
so you can get the current version of WC viaWC()->version
. Also, I think you need to flip your comparison operator around. Untested, but I think this would work.