13

I am trying to modify the head and foot of my WooCommerce pages. I have an if statement that is supposed to target the shop and cart of WooCommerce, but it isn't. If I modify the PHP after the if statement nothing changes. But if I modify the PHP in the else statement is works:

This doesn't work:

<?php if (function_exists('woocommerce')): ?>
    <?php if (is_cart() || is_shop()): ?>
        <?php get_template_part('inc/CHANGE'); ?>
    <?php endif ?>
<?php else: ?>
    <?php get_template_part('inc/page-header'); ?>
<?php endif ?>

This does work:

<?php if (function_exists('woocommerce')): ?>
    <?php if (is_cart() || is_shop()): ?>
        <?php get_template_part('inc/page-header'); ?>
    <?php endif ?>
<?php else: ?>
    <?php get_template_part('inc/CHANGE'); ?>
<?php endif ?>

I think the function WooCommerce might not be properly defined, because this also works:

<?php if (is_cart() || is_shop()): ?>
        <?php get_template_part('inc/header-shop'); ?>
    <?php else: ?>
        <?php get_template_part('inc/page-header'); ?>
    <?php endif ?>
0

7 Answers 7

26

Your edit got me to this idea, there indeed is no function called »woocommerce«, there is a class »WooCommerce« though. One thing to be aware of is, that the check has to late enough, so that plug-ins are actually initialized, otherwise - obviously - the class won't exists and the check returns false. So your check should look like this:

if ( class_exists( 'WooCommerce' ) ) {
  // some code
} else {
  / more code
}

On the WooCommerce documentation page »Creating a plugin for WooCommerce« I have found this:

/**
 * Check if WooCommerce is active
 **/
if ( 
  in_array( 
    'woocommerce/woocommerce.php', 
    apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) 
  ) 
) {
    // Put your plugin code here
}

Personally I think it is not nearly as reliable as checking for the class. For obvious reasons, what if the folder/file name is different, but should work just fine too. Besides if you do it like this, then there is an API function you can use, fittingly named is_plugin_active(). But because it is normally only available on admin pages, you have to make it available by including wp-admin/includes/plugin.php, where the function resides. Using it the check would look like this:

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
  // some code
} else {
  / more code
}
7
  • 3
    This could be a fun "party" game, finding other ways ;-) Let me join in with if( function_exists( 'WC' ) ), but it's more accurate to have the plugin name in the class check, as you suggested ;-)
    – birgire
    Commented Jul 8, 2015 at 23:05
  • 2
    Actually this function WC() exists, I was just trying to find a shorter way by referring to it ;-) I guess it would be more accurate with if( function_exists( 'WC' ) && is_object( WC() ) && is_a( WC(), 'WooCommerce' ) ) ;-). Playing along, we could also test for constants ( e.g. WOOCOMMERCE_VERSION ) or actions (e.g. did_action( 'woocommerce_loaded' ) ) ;-) @ialocin
    – birgire
    Commented Jul 9, 2015 at 8:28
  • 1
    True, at the end of the day there are a lot of possibilities! @birgire Commented Jul 9, 2015 at 8:32
  • 1
    Yeah, go it :) So WPSE is actually kind of is the online version of »99 ways to solve your PHP/WorPress problems« - I had to coin it, to make it official :) @birgire Commented Jul 9, 2015 at 8:46
  • 2
    This answer IS NOT reliable. Since neither the question nor the answer explicitly state the context, it is possible that WooCommerce is active, but loaded after this code. For example in another plugin, that happens to get loaded before. The suggested class check is much more reliable, if you additionally wrap it in plugins_loaded hook. I.e. wrap the if-clause in a check_for_woocommerce() function, and register it with add_action('plugins_loaded', 'check_for_woocommerce');
    – kontur
    Commented Oct 30, 2017 at 13:27
7

Many of the official WooCommerce plugins solve this by checking for the WC_VERSION constant, which WooCommerce defines, once all plugins have loaded. Simplified code:

add_action('plugins_loaded', 'check_for_woocommerce');
function check_for_woocommerce() {
    if (!defined('WC_VERSION')) {
        // no woocommerce :(
    } else {
        var_dump("WooCommerce installed in version", WC_VERSION);
    }
}

The added bonus is that you can use PHP's version_compare() to further check if a new enough version of WooCommerce is installed (if you code requires specific capabilities), since the WC_VERSION constant is suitable for this.

1
  • this is a really good way. Good tip for plugin developers to include as well. Commented Jan 29, 2018 at 15:11
4

To improve on the answers given, we're using this:

$all_plugins = apply_filters('active_plugins', get_option('active_plugins'));
if (stripos(implode($all_plugins), 'woocommerce.php')) {
    // Put your plugin code here
}

This prevents two problems:

  • WooCommerce being installed in a non-standard directory - in which case if ( in_array( 'woocommerce/woocommerce.php', apply_filters(... doesn't work.
  • This check being invoked before WooCommerce is loaded - in which case if ( class_exists( 'WooCommerce' ) ) { .. } doesn't work.
2
  • This answer is useful for plugin developers since you can just wrap your entire plugin main file with this IF statement. Commented Apr 30, 2019 at 8:59
  • This was the only solution that worked for me in my plugin! Thank you!!!
    – Sam
    Commented May 18, 2019 at 22:12
3

I found this useful from the WooCommerce developer documentation.

You can simply call is_woocommerce_active() function within your plugin file.

Example

if ( ! is_woocommerce_active() ) 
{
    add_action( 'admin_notices', 'WC_Subscriptions::woocommerce_inactive_notice' );
    return;
}

is_woocommerce_active defined as below as per woo documentation

/**
 * Check if WooCommerce is activated
 */
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
    function is_woocommerce_activated() {
        if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; }
    }
}
2

Solution that I use in 2021:

add_action( 'admin_init', 'my_plugin_check_if_woocommerce_installed' );
function my_plugin_check_if_woocommerce_installed() {

    // If WooCommerce is NOT installed, Deactivate the plugin
    if ( is_admin() && current_user_can( 'activate_plugins') && !is_plugin_active( 'woocommerce/woocommerce.php') ) {

        // Show dismissible error notice
        add_action( 'admin_notices', 'my_plugin_woocommerce_check_notice' );

        // Deactivate this plugin
        deactivate_plugins( plugin_basename( __FILE__) );
        if ( isset( $_GET['activate'] ) ) {
            unset( $_GET['activate'] );
        }
    }
    // If WooCommerce is installed, activate the plugin and carry out further processing
    elseif ( is_admin() && is_plugin_active( 'woocommerce/woocommerce.php') ) {

        // Carry out further processing here
    }
}

// Show dismissible error notice if WooCommerce is not present
function my_plugin_woocommerce_check_notice() {
    ?>
    <div class="alert alert-danger notice is-dismissible">
        <p>Sorry, but this plugin requires WooCommerce in order to work.
            So please ensure that WooCommerce is both installed and activated.
        </p>
    </div>
    <?php
}

The code runs on 'admin_init', rather than on 'register_activation_hook' because it needs WooCommerce to be loaded and detected successfully. So if WooCommerce is NOT detected, it does the following:

a) Shows a dismissible error notice to Admin about the absence of WooCommerce. This is very graceful as it does not use 'wp_die'.

b) Deactivates your custom plugin automatically.

If WooCommerce is detected, you may proceed with adding menu links, etc. as needed.

CAVEATS:

  1. The above solution works on the assumption that the core name of WooCommerce plugin and its main file remains "woocommerce". If that were to change anytime in the future, then the checks will break. So in order to make this code work at that time, the then current WooCommerce directory name and main file name should be updated in the above code, within the if check and elseif check.

  2. Do note that if both WooCommerce and your plugins are installed and running successfully, then if you were to deactivate WooCommerce anytime for any reason, then it would automatically deactivate your plugin as well.

1

This is for multisite support.

$is_active = in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );

if ( is_multisite() ) {
    if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
        include_once ABSPATH . 'wp-admin/includes/plugin.php';
    }
    if ( function_exists( 'is_plugin_active_for_network' ) ) {
        $is_active = is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) || $is_active;
    }
}

if ( $is_active ) {

}
0

You can wrap your plugin in a check to see if WooCommerce is installed and active or not :

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
echo 'WooCommerce is active.';
} else {
echo 'WooCommerce is not Active.';
}
2
  • 3
    This doesn't work with multisites Commented Oct 20, 2016 at 11:38
  • @AndrewLazarus Thank you for mentioning that - I just lost an hour to this problem! :)
    – wickywills
    Commented Jan 2, 2019 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.