How to show admin notices when checking WooCommerce version

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');

Related posts

Leave a Reply

1 comment

  1. 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 main WooCommerce class. You can now get the singleton instance of that class via the function WC(). And one of the class variables is version so you can get the current version of WC via WC()->version. Also, I think you need to flip your comparison operator around. Untested, but I think this would work.

    function check_wc_version($version = '2.6.1'){
        if ( function_exists( 'WC' ) && ( version_compare( WC()->version, $version, "<" ) ) {
                echo 'You need a higher version of WooCommerce';
        }
    }
    add_action('admin_notices', 'check_wc_version');