9

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 ?>
15

Note: For a new, more complete, better and more or less official way please take a look at kontur's answer. As he commented, this could be done with the class check too.


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
}
  • 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 Jul 8 '15 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 Jul 9 '15 at 8:28
  • 1
    True, at the end of the day there are a lot of possibilities! @birgire – Nicolai Jul 9 '15 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 – Nicolai Jul 9 '15 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 Oct 30 '17 at 13:27
6

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.

  • this is a really good way. Good tip for plugin developers to include as well. – Jacob Raccuia Jan 29 '18 at 15:11
3

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.
  • This answer is useful for plugin developers since you can just wrap your entire plugin main file with this IF statement. – Dave Hilditch Apr 30 '19 at 8:59
  • This was the only solution that worked for me in my plugin! Thank you!!! – Sam May 18 '19 at 22:12
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
    This doesn't work with multisites – Andrew Lazarus Oct 20 '16 at 11:38
  • @AndrewLazarus Thank you for mentioning that - I just lost an hour to this problem! :) – wickywills Jan 2 '19 at 13:27
0

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; }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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