Skip to content Skip to main menu
User Role Editor
Menu
  • Home
  • Prices
  • Download
  • Testimonials
  • Forums
  • Blog
  • ChangeLog
  • Documentation
  • Login
Home › Topic › WooCommerce – My account -page restrict access to tabs

Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › WooCommerce – My account -page restrict access to tabs

Tagged: WooCommerce - My account -page

  • This topic has 7 replies, 2 voices, and was last updated 8 years, 8 months ago by Vladimir.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • 30/09/2016 at 12:47 #2812
    UranusOy
    Participant

    Hi,

    I was wondering how could I get user role based restrictions to Woocommerces my account -page.

    I have couple of roles that will have access to “my account” -page. First is “Employer” witch has access to “Dashboard, Orders, Addresses, Account Details, Logout” -tabs on “My Account” -page.

    Second one is “Candidate” and I want to restrict candidates access on Woocommerces “My Account” -page to “Account Details” and “Logout”

    30/09/2016 at 14:53 #2816
    Vladimir
    Keymaster

    Hi,

    Try to install this code as a “must use” plugin. Replace ‘subscriber’ role inside to you own role ID, e.g. ‘candidate’:

    
    add_filter('woocommerce_account_menu_items', 'filter_wc_my_account_menu');
    add_action('template_redirect', 'redirect_for_blocked_wc_pages');
    
    function filter_wc_my_account_menu($items) {
        if (!current_user_can('subscriber')) {
            return $items;
        }
        if (isset($items['dashboard'])) {
            unset($items['dashboard']);
        }
        if (isset($items['orders'])) {
            unset($items['orders']);
        }
        if (isset($items['downloads'])) {
            unset($items['downloads']);
        }
        if (isset($items['edit-address'])) {
            unset($items['edit-address']);
        }
        if (isset($items['payment-methods'])) {
            unset($items['payment-methods']);
        }
    
        return $items;
    }
    
    function check_end_point_url($end_point, $current_url) {
        $blocked_url = wc_get_endpoint_url($end_point);
        if ($current_url==$blocked_url) {
            $my_account_url = wc_get_endpoint_url('woo-account-page');
            wp_redirect($my_account_url);
            die;
        }
    }
    
    function redirect_for_blocked_wc_pages() {
        global $wp;
        
        if (!current_user_can('subscriber')) {
            return;
        }
            
        $current_url = trailingslashit(home_url($wp->request));        
        $blocked_end_points = array('dashboard', 'orders', 'downloads', 'edit-address', 'payment-methods');
        foreach($blocked_end_points as $bep) {
            check_end_point_url($bep, $current_url);
        }
        
    }
    
    03/10/2016 at 12:43 #2829
    UranusOy
    Participant

    Almost perfect.

    Only thing is that the dashboard is still the first page when you open “My Account” page so you will see all dashboard content. It would be preferable to show “Account Details” pages as default page “My Account” -page instead of “Dashboard”. This would be for “Candidates” of course.

    Thank you so much for your time and effort
    Great product and great service

    03/10/2016 at 16:08 #2835
    Vladimir
    Keymaster

    Thanks for the feedback. I will look if it’s possible to change the default page for “My Account”.

    04/10/2016 at 04:57 #2836
    UranusOy
    Participant

    Thank you,

    This is very much appreciated.

    04/10/2016 at 11:24 #2838
    Vladimir
    Keymaster

    Try this variant. I removed the “Account Details” from the left menu items too, as it’s the only available my account endpoint and it’s not sense to place the link on the same page.

    
    add_filter('woocommerce_account_menu_items', 'filter_wc_my_account_menu');
    add_action('template_redirect', 'redirect_for_blocked_wc_pages');
    
    function filter_wc_my_account_menu($items) {
        if (!current_user_can('subscriber')) {
            return $items;
        }
        if (isset($items['dashboard'])) {
            unset($items['dashboard']);
        }
        if (isset($items['orders'])) {
            unset($items['orders']);
        }
        if (isset($items['downloads'])) {
            unset($items['downloads']);
        }
        if (isset($items['edit-address'])) {
            unset($items['edit-address']);
        }
        if (isset($items['edit-account'])) {
            unset($items['edit-account']);
        }
        if (isset($items['payment-methods'])) {
            unset($items['payment-methods']);
        }
    
        return $items;
    }
    
    function redirect_from_blocked_url() {
        $my_account_url = wc_get_endpoint_url('edit-account');
        wp_redirect($my_account_url);
        die;    
    }
    
    function check_end_point_url($end_point, $current_url) {
        $blocked_url = wc_get_endpoint_url($end_point);
        if ($current_url==$blocked_url) {
            redirect_from_blocked_url();
        }
    }
    
    function redirect_for_blocked_wc_pages() {
        global $wp, $wp_query;
               
        if (!current_user_can('subscriber')) {
            return;
        }
        
        if (is_account_page() && !is_wc_endpoint_url()) {
            // block Woo My Account Dashboard;
            redirect_from_blocked_url();
        }
                
        $current_url = trailingslashit(home_url($wp->request));
        $blocked_end_points = array('dashboard', 'orders', 'downloads', 'edit-address', 'payment-methods');
        foreach($blocked_end_points as $bep) {
            check_end_point_url($bep, $current_url);
        }
        
    }
    
    04/10/2016 at 11:52 #2839
    UranusOy
    Participant

    It works perfectly.

    Thank you so much for your help.

    Great plugin and great support.

    and for anyone else reading this post I would definitely recommend this plugin.

    05/10/2016 at 02:20 #2844
    Vladimir
    Keymaster

    Thanks for the feedback.

  • Author
    Posts
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.
Log In

Recent Topics

  • Role editor widget has vanished from pages in admin
  • Issue with Image Insertion in Posts – User Role Editor Pro
  • Item not available in Admin Menu
  • Restrict by Taxonomy, but Allow Access to all Taxonomy Items
  • Editor role – Siteground Speed Optmiser – Flush Cache
  • Editor Access to Appearance / Menus
  • WP Version 6.8 – Text Domain Error
  • Prevent Unauthenticated Access to CPT by Default
  • Other roles not restricted
  • Admin not having WP backend menu items

Recent Replies

  • [email protected] on Item not available in Admin Menu 1 month, 1 week ago
  • Vladimir on Item not available in Admin Menu 1 month, 1 week ago
  • VC Team on Admins can’t see new CPTs 1 month, 2 weeks ago
  • Stephen001 on Editor Access to Appearance / Menus 1 month, 3 weeks ago
  • Vladimir on Editor Access to Appearance / Menus 1 month, 3 weeks ago
  • Vladimir on WP Version 6.8 – Text Domain Error 2 months, 1 week ago
  • Vladimir on WP Version 6.8 – Text Domain Error 2 months, 1 week ago
  • camber799 on Prevent Unauthenticated Access to CPT by Default 2 months, 3 weeks ago
  • Channel Digital on Multisite – Capabilities reset when restricting access to a plugin 3 months, 1 week ago
  • Channel Digital on Multisite – Capabilities reset when restricting access to a plugin 3 months, 2 weeks ago
  • Contact Us
  • Privacy Policy
  • Terms of use (EULA)
© 2010 - 2025 Artellect Limited. Units A-C, 25/F., Seabright Plaza, No. 9-23 Shell Street, North Point, Hong Kong. Contact: hd@role-editor.com
Share