0

https://wordpress.org/plugins/woocommerce-cart-stock-reducer

I installed above mentioned plugin on a wordpress website. But I need to limit the functionality of this plugin to a group of users (i.e. UserRole=B2B)

How can I restrict functionality of a plugin to only certain group of users?

2
  • which functionality of plugin, furnish the code here
    – Rasa Mohamed
    Commented Nov 17, 2017 at 9:04
  • Functionality of this plugin is that while adding a product to the cart, it reduces its inventory and reserves it for a specified period of time. i.e. 24 hours/ After this period, if the payment has been made, it will reduce the inventory definitively, and if the payment has not been made, it will return the inventory back to the warehouse./ I want that this plugin do this, just for a specific user group./ Also please consider that I'm not technical.
    – Hamid
    Commented Nov 17, 2017 at 10:14

1 Answer 1

0

Usually, you would not check the user role, but the user's capabilities via current_user_can, and use a plugin such as User Role Editor to add the custom capabilities to certain roles. This is much more flexible to use in the long term, see Roles and capabilities (for example, do you want your wordpress admin user to behave like this or not?). Normally a plugin would have checks like:

if (!current_user_can('my_plugin_cap')) {
 wp_die('Not allowed');
}

Now in your case the challenge would probably be how to add this in a way where the plugin does not need to be modified. As the functionality the plugin is adding is often added via wordpress actions/filters (woocommerce works like this), you can remove it like this:

add_action('woocommerce_integrations', 'my_check_capability', 1); // Adding this before the plugin is called
function my_check_capability() {
  if (!current_user_can('my_plugin_cap')) {
   remove_action('woocommerce_integrations', 'make_cart_stock_reducer_go_now' ); // Disable the plugin, do as if the plugin didn't exist, if not enough capabilities
  }
}

This code needs to be added in your theme's functions.php or so.

Your Answer

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