## When I deactive WooCommerce Plugin, I want to take a action in My plugin

Question

I have a WordPress a Plugin which is dependent by WooCommerce. When a user uninstall or deactive wooCommerce plugin, in this time, I mean when woocommerce plugin register_deactivation_hook, I want to take a notice in my active current pluing. How can i do this ? pleas Help.

in progress 0
1 year 2021-08-05T11:02:13-05:00 0 Answer 0 views 0

1. There’s two ways to do this, you can either check for the main plugin file or you can check for the WooCommerce class. When I make a plugin dependent on some other plugin I usually check for the plugin file, but for WooCommerce I check for the WooCommerce class.

<?php
function yourplugin_init() {
if( !function_exists( 'is_plugin_inactive' ) ) :
endif;
//COMMON WOOCOMMERCE METHOD
if( !class_exists( 'WooCommerce' ) ) :
//ALTERNATIVE METHOD
//if( is_plugin_inactive( 'woocommerce/woocommerce.php' ) ) :
function yourplugin_deactivate() {
deactivate_plugins( plugin_basename( __FILE__ ) );
}
echo '<div class="error"><p><strong>WooCommerce</strong> must be installed and activated to use this YOURPLUGIN.</p></div>';
if( isset( $_GET['activate'] ) ) unset($_GET['activate'] );
}
endif;
}
?>


First, put the above code in your primary plugin file before any of the custom functionality you’re adding.

We create a function that we run on plugins_loaded; it checks if the WP is_plugin_inactive function executes, if not, it loads the WP plugin file.

Next we check if a) the WooCommerce class does NOT exist or b) the alternative method which I’ve commented out, we check if the WooCommerce plugin is INACTIVE using the plugin’s main PHP file (ie. woocommerce/woocommerce.php).

Now, IF we’ve established that WooCommerce isn’t active (using one of the two methods above) we hook into two separate actions. admin_init admin_notices

Then we have two functions yourplugin_deactivate() and yourplugin_admin_notice() which are pretty self explanatory. We deactivate the your plugin and then we post a notice at the top of the admin screen telling the user what we’ve done and why.

And there you go…