WooCommerce: Disable A Plugin For Customers / Shop Managers

Ok, this is an unusual snippet today, but it may happen that for performance / security / conflict / conditional / privacy reasons you may need a certain WooCommerce user role to not see / load / use a given plugin.

Let’s think of an example: as an administrator, you wish to use a CRM plugin to sync your order data to an external software. This plugin, however, does not have the ability to exclude Shop Managers from accessing it, and you don’t want to install yet another plugin to define who can access and who can not.

Another case scenario: Shop Managers and Administrators wish to use a live chat plugin, but they want to restrict the live chat visibility to logged in customers only, while logged out customers should not see anything, hidden code included.

There are a million reasons why this could be helpful. So, let’s see how to actually deactivate a plugin (not disable its scripts – but actually deactivate it) with a handy piece of code. Test it and only then – enjoy!

Here’s a new function you’ll learn about today – deactivate_plugins. You can actually completely disable a plugin via code, and the snippet below gives you a practical example.

PHP Snippet: Conditionally Deactivate / Activate Plugin Based on Logged In User Role

Lots to learn here:

In regard to the actual deactivation / activation process, please leave a comment below if it worked for you. I know I had problems related to object cache that I wasn’t able to solve, but if it works for you then we’re in business!

You can test if this works by logging in with the specific user role (Shop Manager in the example below), go to the Plugins page in the WordPress dashboard, and see if the plugin is in fact deactivated.

/**
 * @snippet       Activate / Deactivate Plugin By Current User Role
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_action( 'init', 'bbloomer_deactivate_plugin_for_shop_managers' );

function bbloomer_deactivate_plugin_for_shop_managers() {
	if ( wp_doing_ajax() ) return;
	if ( wc_current_user_has_role( 'shop_manager' ) ) {
		deactivate_plugins(
         array( 'fluid-checkout/fluid-checkout.php' ),
			true,
         false,
      );
	} else {
		activate_plugins(
         array( 'fluid-checkout/fluid-checkout.php' ),
			'',
         false,
         true,
      );
   }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Disable A Plugin For Customers / Shop Managers

  1. Nice one, it’s just worth to highlight that ‘$silent’ parameter should be ‘true’ to stop activation/deactivation hooks. You don’t want these hooks running all the time.

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *