Search code examples
phppluginswoocommerceuser-roles

PHP How to able or disable a plugin or plugin functionality (WooCommerce deposits) based on user role?


I am building a private distributor WooCommerce store with woo private store plugin from Barn2 media.

This store is part of a website with a public area where customers can apply for a distributor account by filling out an application form. The store will be used by two different type of distributors for which I have created two user roles accordingly:

1) Retailer

2) Wholesaler.

These two roles were originally created with Advanced Access Manager. Ever since I have uninstalled this plugin and installed Profile Builder Pro instead as I needed a plugin that could perfectly integrate user roles and forms. The application form was actually created with this plugin.

The reason for this is that once submitted, then the application is reviewed and the account manually approved/activated by the admin, using one of the profile or the other depending on the profile and business requirements of the applicant. This is exactly what Profile Builder allows you to do.

Once logged in, products on the single product page show different pricings based on the user profile. For this feature I am using the Price based on User Role function of the booster for WooCommerce plugin. I have then installed the WooCommerce deposits plugin to arrange different payment procedures also based on user roles on the check-out page.

However, the plugin does not allow to have different payment settings based on user roles. The payment procedure for Retailer users should be straight forward/regular, that is to say full payment must be made upon order. For Retailer users supply relies on an existing inventory.

For Wholesaler users nevertheless, goods are produced upon order confirmation. To have an order confirmed, a deposit payment of a certain % (let’s say 40%) should be made first. Once the goods are produced and ready for shipping the balance % (let’s say 60%) of the total amount will have to be settled so the goods may be released.

My challenge is now the following one: The plugin is perfect to set up the payment procedure for Wholesaler users and I want to keep as it is. However how can I completely disable it or its functionality when the logged in user is a Retailer?


Solution

  • The function deactivate_plugin() will help you to do it, for example:

    deactivate_plugins( '/plugins/xxx.php' );
    

    where xxx is the name of the plugin.

    And, to check the capability of the user:

    if (in_array('Administrator', $current_user->roles)) {
    

    here example for admin role.

    The complete snippet:

    add_action('admin_init', 'desactivate_plugin_role');    
    function desactivate_plugin_role()
    {
        global $current_user;
        if (in_array('Administrator', $current_user->roles)) {
            deactivate_plugins( '/plugin-folder/plugin-file.php' );
        } else { 
            activate_plugins( '/plugin-folder/plugin-file.php' );
        }
    }
    

    Tested, works for me, don't forget to change the user role, path and file of the plugin.