简体   繁体   English

在 WooCommerce 中添加基于用户角色的自定义我的帐户菜单项 3+

[英]Add custom my account menu item based on user role in WooCommerce 3+

I am using WordPress and WooCommerce and I have followed this article https://rudrastyh.com/woocommerce/my-account-menu.html to add new menu items in WooCommerce my account menus. I am using WordPress and WooCommerce and I have followed this article https://rudrastyh.com/woocommerce/my-account-menu.html to add new menu items in WooCommerce my account menus.

This is my working code.这是我的工作代码。

function getUserRolesByUserId( $id ) {
    
    if ( !is_user_logged_in() ) { return false; }

    $oUser = get_user_by( 'id', $id );
    $aUser = get_object_vars( $oUser );
    $sRoles = $aUser['roles'];
    return $sRoles;

}

function createMenuBasedonUserRole($userId)
{
    $userRoleIds = getUserRolesByUserId(get_current_user_id());

    $urlMenuData = [];
    if(!empty($userRoleIds) && in_array('mindesk_var_account',$userRoleIds)) {
        
        $urlMenuData = [
            'pageName' => "Clients",
            "pageLink" => "clients"
        ];
        
        
 
    } else if(!empty($userRoleIds) && in_array('mindesk_owner_account',$userRoleIds)) {

        $urlMenuData = [
            'pageName' => "Children",
            "pageLink" => "children"
        ];  
        
    }
    return $urlMenuData;
}

/*
 * Step 1. Add Link (Tab) to My Account menu
 */
add_filter ( 'woocommerce_account_menu_items', 'mindesk_clients_children_link', 40 );

function mindesk_clients_children_link( $menu_links ){
    $urlData = createMenuBasedonUserRole(get_current_user_id());
    
    if(!empty($urlData)){
        $menu_links = array_slice( $menu_links, 0, 5, true ) + array( $urlData['pageLink'] => $urlData['pageName'] ) + array_slice( $menu_links, 5, NULL, true );
    }
    
    return $menu_links;

}

/*
* Step 2. Register Permalink Endpoint
*/
add_action( 'init', 'mindesk_add_menu_endpoint' );
function mindesk_add_menu_endpoint() {  
        
    add_rewrite_endpoint( 'clients', EP_PAGES );
    add_rewrite_endpoint( 'children', EP_PAGES );
}

/*
* Step 3. Content for the new page in My Account, woocommerce_account_{ENDPOINT NAME}_endpoint
*/
add_action( 'woocommerce_account_clients_endpoint', 'mindesk_clients_my_account_endpoint_content' );
function mindesk_clients_my_account_endpoint_content() {
    require_once(get_template_directory() . '/myaccount/clients.php') ;
}

add_action( 'woocommerce_account_children_endpoint', 'mindesk_children_my_account_endpoint_content' );
function mindesk_children_my_account_endpoint_content() {
    require_once(get_template_directory() . '/myaccount/children.php') ;
}
/* Step 4
*/
// Go to Settings > Permalinks and just push "Save Changes" button.

And this is my how my new menu called as "Clients" showing.这就是我称为“客户”的新菜单的显示方式。

在此处输入图像描述

As you can see above, I have added new menu and executing the page and based on user role mindesk_var_account I need to show clients and mindesk_owner_account I need to show children .正如您在上面看到的,我添加了新菜单并执行了页面,并且基于用户角色mindesk_var_account我需要显示clientsmindesk_owner_account我需要显示children

I have created these 2 php pages at /wp-content/themes/twentytwentyone/myaccount and its working fine.我在/wp-content/themes/twentytwentyone/myaccount创建了这 2 个 php 页面,并且工作正常。

However, I want to use wp_die or something if user with another role try to access one of the page which they are not allowed to.但是,如果具有其他角色的用户尝试访问他们不允许访问的页面之一,我想使用wp_die或其他东西。

So for example if logged in user has mindesk_var_account role then if they try to go to http://localhost/wordpress/my-account/clients/ then i need to use wp_die() to not execute it.因此,例如,如果登录用户具有mindesk_var_account角色,那么如果他们尝试从 go 到 http://localhost/wordpress/my-account/clients/ 那么我需要使用wp_die()不执行它。

I tried to use wp_die inside these new 2 pages but then menus and other things executed.我尝试在这两个新页面中使用wp_die ,但随后执行了菜单和其他内容。 I just want something like this.我只是想要这样的东西。

在此处输入图像描述

I tried to use following code...我尝试使用以下代码...

add_action( 'template_redirect', 'my_account_redirect' );
function my_account_redirect() {
    if( is_page( 'my-account' ) ) {
        wp_die('fg');

    }
}

But then its checking for all my-account pages.. and I want it to be checked only for inner pages like client or children .但随后它检查所有my-account页面.. 我希望它只检查内部页面,如clientchildren

Can someone guide me how can I achieve this what should I do from here on.有人可以指导我如何实现这一点,从这里开始我应该做什么。

Thanks谢谢

There are still some little mistakes in your code, some missing things and since WooCommerce 3 there are some related changes within step 2 for My account endpoints.您的代码中仍然存在一些小错误,缺少一些东西,并且由于 WooCommerce 3 在我的帐户端点的第 2 步中有一些相关的更改。 Some things can be simplified too.有些事情也可以简化。

To avoid non allowed user roles to access to some prohibited section(s) or endpoint(s) you can use a custom function hooked in template_redirect hook that will redirect user to an allowed section.为了避免不允许的用户角色访问某些禁止的部分或端点,您可以使用自定义 function 挂钩在template_redirect钩子中,它将用户重定向到允许的部分。

Here is the complete code:这是完整的代码:

// Custom function that get My account menu item data based on user roles
function get_menu_item_by_user_role() {
    $user_roles = wp_get_current_user()->roles;

    if ( ! empty($user_roles) ) {
        $menu_item = [];

        // if ( in_array('mindesk_var_account', $user_roles) ) {
        if ( in_array( 'mindesk_var_account', $user_roles ) ) {
            $menu_item = [ 'clients' => __( "Clients", "woocommerce" ) ];
        }
        elseif( in_array( 'mindesk_owner_account', $user_roles ) ) {
            $menu_item = [ 'children' => __( "Children", "woocommerce" ) ];
        }
    }
    return $menu_item;
}

// Step 1 - Add Link (Tab) to My Account menu
add_filter ( 'woocommerce_account_menu_items', 'add_mindesk_custom_menu_items', 40 );
function add_mindesk_custom_menu_items( $menu_items ){
    $new_item = get_menu_item_by_user_role();

    if ( ! empty($new_item) ) {
        $menu_items = array_slice( $menu_items, 0, 5, true ) + $new_item + array_slice( $menu_items, 5, null, true );
    }
    return $menu_items;
}

// Step 2 - Enable endpoint (and endpoint permalink) - Since WooCommerce 3
add_filter( 'woocommerce_get_query_vars', 'add_mindesk_menu_item_endpoint' );
function add_mindesk_menu_item_endpoint( $query_vars ) {
    $query_vars['clients']  = 'clients';
    $query_vars['children'] = 'children';

    return $query_vars;
}

// Step 3. Content for the new page in My Account, woocommerce_account_{ENDPOINT NAME}_endpoint
add_action( 'woocommerce_account_clients_endpoint', 'add_mindesk_account_clients_endpoint_content' );
function add_mindesk_account_clients_endpoint_content() {
    require_once(get_template_directory() . '/myaccount/clients.php') ;
}

add_action( 'woocommerce_account_children_endpoint', 'add_mindesk_account_children_endpoint_content' );
function add_mindesk_account_children_endpoint_content() {
    require_once(get_template_directory() . '/myaccount/children.php') ;
}

// Step 4. Endpoint page title
add_filter( 'woocommerce_endpoint_clients_title', 'set_mindesk_account_clients_endpoint_title', 10, 2 );
function set_mindesk_account_clients_endpoint_title( $title, $endpoint ) {
    $title = __("Clients", "woocommerce" );

    return $title;
}

add_filter( 'woocommerce_endpoint_children_title', 'set_mindesk_account_children_endpoint_title', 10, 2 );
function set_mindesk_account_children_endpoint_title( $title, $endpoint ) {
    $title = __( "Children", "woocommerce" );

    return $title;
}

// Step 5. Redirect if not allowed user role
add_action( 'template_redirect', 'redirect_mindesk_account_dashboard' );
function redirect_mindesk_account_dashboard() {
    if ( is_account_page() ) {
        global $wp;

        $item_key = array_keys(get_menu_item_by_user_role());
        $page_url = get_permalink( get_option('woocommerce_myaccount_page_id') );

        if ( empty($item_key) &&  ( isset($wp->query_vars['children']) || isset($wp->query_vars['clients']) ) ) {
            wp_safe_redirect( get_permalink($page_id) );
            exit();
        }
        elseif ( 'clients' == reset($item_key) && isset($wp->query_vars['children']) ) {
            wp_safe_redirect( get_permalink($page_id) . 'clients/' );
            exit();
        }
        elseif ( 'children' == reset($item_key) && isset($wp->query_vars['clients']) ) {
            wp_safe_redirect( get_permalink($page_id) . 'children/'  );
            exit();
        }
    }
}

// Step 6. FLush rewrite rules:
// Go to Settings > Permalinks and click on "Save Changes".

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

related: WooCommerce My Account custom endpoint menu item相关: WooCommerce 我的帐户自定义端点菜单项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 我的帐户菜单中的注销项上方添加自定义项 - Add a custom item above logout item in Woocommerce my account menu 在 Woocommerce 中为我的帐户订单表添加自定义列 3+ - Add a custom column to My Account Orders table in Woocommerce 3+ 在WooCommerce中定义用户变量我的账户自定义菜单项内容 - Define user variable in WooCommerce My account custom menu item content WooCommerce 我的帐户自定义端点菜单项 - WooCommerce My Account custom endpoint menu item 根据用户角色显示自定义主题“my-account.php”woocommerce 页面 - Display a custom themed "my-account.php" woocommerce page based on user role 根据Woocommerce中的特定用户角色显示菜单项 - Show a menu Item based on specific User role in Woocommerce WooCommerce 我的帐户页面自定义菜单项与自定义链接 - WooCommerce My Account page custom menu item with custom link 仅针对特定用户角色在 Woocommerce 中自定义我的帐户端点 - Custom my account endpoint in Woocommerce just for a specific user role 如何将链接项目添加到 WooCommerce 我的帐户菜单中的另一个页面 - How to add a linked item to another page in WooCommerce My Account menu 在 WooCommerce 我的帐户页面上,根据特定用户角色显示我的地址部分 - On WooCommerce My account page, display My Addresses section based on specific user role
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM