Change WordPress user roles and capabilities Forums Give user access to plugin – how to Need to give a user access to specific page on WooCommerce

Tagged: 

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #6171
    lachero
    Participant

    Hey, How do I grant access only to the “Payments” page on WooCommerce? wp-admin/admin.php?page=wc-settings&tab=checkout
    http://prntscr.com/q6pflt

    Thanks

    #6179
    Vladimir
    Keymaster

    Hi,

    Setup this code as Must Use plugin or add it to your active Theme. It leaves only 2 tabs at WC->Settings page: General and Payments. And replace ‘orders_manager’ role ID with your own one.
    In order to leave Payments only, you need apply more programming efforts, replace link of WC-Settings menu from default one to
    wp-admin/admin.php?page=wc-settings&tab=checkout

    
    add_filter('woocommerce_settings_tabs_array', 'change_wc_settings_tabs', 30);
    
    function change_wc_settings_tabs( $pages ) {
        
    
        $user = wp_get_current_user();
        if ( in_array( 'orders_manager', $user->roles ) ) {
            //unset( $pages['general']);      // General
            unset( $pages['products']);     // Products
            unset( $pages['tax']);          // Tax
            //unset( $pages['checkout']);       // Payments
            unset( $pages['account']);        // Accounts & Privacy
            unset( $pages['email']);          // Emails
            unset( $pages['advanced']);       // Advanced
        }
      
      return $pages;
    }
    
    #6869
    [email protected]
    Participant

    Hi

    What will the code look like if we want to exclude the Payments tab but keep all of the other tabs?

    #6871
    Vladimir
    Keymaster

    Hi,

    Comment lines with tabs to leave, and uncomment lines with tabs you wish to hide/remove.

    
    add_filter('woocommerce_settings_tabs_array', 'change_wc_settings_tabs', 30);
    
    function change_wc_settings_tabs( $pages ) {
        
    
        $user = wp_get_current_user();
        if ( in_array( 'orders_manager', $user->roles ) ) {
            //unset( $pages['general']);      // General
            //unset( $pages['products']);     // Products
            //unset( $pages['tax']);          // Tax
            unset( $pages['checkout']);       // Payments
            //unset( $pages['account']);        // Accounts & Privacy
            //unset( $pages['email']);          // Emails
            //unset( $pages['advanced']);       // Advanced
        }
      
      return $pages;
    }
    
    #6900
    [email protected]
    Participant

    Thanks for the good support.

    #6901
    [email protected]
    Participant

    And if I want to apply it to multiple user roles? I am not a coder…

    #6902
    Vladimir
    Keymaster

    Just replace roles in the $roles array with your own roles ID:

    
    add_filter('woocommerce_settings_tabs_array', 'change_wc_settings_tabs', 30);
    
    function change_wc_settings_tabs( $pages ) {
        
        $roles = array(
            'role1',
            'role2',
            'role3'
        );
        $user = wp_get_current_user();
        foreach( $roles as $role ) {
            if ( in_array( $role, $user->roles ) ) {
                //unset( $pages['general']);      // General
                //unset( $pages['products']);     // Products
                //unset( $pages['tax']);          // Tax
                unset( $pages['checkout']);       // Payments
                //unset( $pages['account']);        // Accounts & Privacy
                //unset( $pages['email']);          // Emails
                //unset( $pages['advanced']);       // Advanced
            }
        }
      
      return $pages;
    }
    
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.