简体   繁体   English

WooCommerce:仅针对特定用户角色显示我的帐户链接

[英]WooCommerce: Display My Account link only for specific user role

I want to display the subscription link in the WooCommerce My Account navigation only to a specific user role.我想在 WooCommerce 我的帐户导航中仅显示特定用户角色的订阅链接。 But I couldn't figure out how to change the navigation in that way.但我无法弄清楚如何以这种方式更改导航。

I found a solution to change the order of the menu items.我找到了更改菜单项顺序的解决方案。 And I found a solution to add custom links to the navigation.我找到了一个向导航添加自定义链接的解决方案。 But unfortunately I couldn't combine these two to a working snippet.但不幸的是,我无法将这两者结合到一个工作片段中。

Here's the code to change the order:这是更改顺序的代码:

 function my_account_menu_order() {
    $menuOrder = array(
        'orders'             => __( 'Orders', 'woocommerce' ),
        'downloads'          => __( 'Download', 'woocommerce' ),
        'edit-address'       => __( 'Addresses', 'woocommerce' ),
        'edit-account'      => __( 'Account Details', 'woocommerce' ),
        'customer-logout'    => __( 'Logout', 'woocommerce' ),
        'dashboard'          => __( 'Dashboard', 'woocommerce' ),
    );
    return $menuOrder;
 }
 add_filter ( 'woocommerce_account_menu_items', 'my_account_menu_order' );

Because the links are in an array, I couldn't use if/else to check the user role.因为链接在一个数组中,所以我无法使用if/else来检查用户角色。

Here's an example to add custom links to the navigation: (from here: https://rudrastyh.com/woocommerce/my-account-menu.html#add_with_url )这是向导航添加自定义链接的示例:(来自此处: https : //rudrastyh.com/woocommerce/my-account-menu.html#add_with_url

add_filter ( 'woocommerce_account_menu_items', 'misha_one_more_link' );
function misha_one_more_link( $menu_links ){

    // we will hook "anyuniquetext123" later
    $new = array( 'anyuniquetext123' => 'Gift for you' );

    // or in case you need 2 links
    // $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' );

    // array_slice() is good when you want to add an element between the other ones
    $menu_links = array_slice( $menu_links, 0, 1, true ) 
    + $new 
    + array_slice( $menu_links, 1, NULL, true );


    return $menu_links;


}

add_filter( 'woocommerce_get_endpoint_url', 'misha_hook_endpoint', 10, 4 );
function misha_hook_endpoint( $url, $endpoint, $value, $permalink ){

    if( $endpoint === 'anyuniquetext123' ) {

        // ok, here is the place for your custom URL, it could be external
        $url = site_url();

    }
    return $url;

}

But the subscription endpoint already exists.但是订阅端点已经存在。 So I don't know how to add it in this way.所以我不知道如何以这种方式添加它。

Any ideas?有任何想法吗?

Sorry, I found a solution.抱歉,我找到了解决方案。

The following code does the trick:以下代码可以解决问题:

add_filter ( 'woocommerce_account_menu_items', 'misha_one_more_link' );
function misha_one_more_link( $menu_links ){

    // we will hook "anyuniquetext123" later
    $new = array( 'subscriptions'   => __( 'Subscriptions', 'woocommerce' ), );

    // or in case you need 2 links
    // $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' );

    // array_slice() is good when you want to add an element between the other ones
    $menu_links = array_slice( $menu_links, 0, 1, true ) 
    + $new 
    + array_slice( $menu_links, 1, NULL, true );


    return $menu_links;


}

I just need to add the existing endpoint.我只需要添加现有的端点。

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

相关问题 在 WooCommerce 我的帐户页面上,根据特定用户角色显示我的地址部分 - On WooCommerce My account page, display My Addresses section based on specific user role 在WooCommerce的“我的帐户”仪表板上显示用户角色名称 - Display User Role Name on My Account Dashboard in WooCommerce 仅针对特定用户角色在 Woocommerce 中自定义我的帐户端点 - Custom my account endpoint in Woocommerce just for a specific user role 根据用户角色显示自定义主题“my-account.php”woocommerce 页面 - Display a custom themed "my-account.php" woocommerce page based on user role 根据用户角色更改 Woocommerce 我的帐户选项卡名称 - Change Woocommerce My Account Tab name depending on User Role 为 WooCommerce 的特定用户角色在我的帐户仪表板中显示附加文本 - Display Additional Text in My Account's Dashboard for Specific User Roles for WooCommerce 仅当项目属于特定类别时才显示 WooCommerce 我的帐户订单 - Display WooCommerce My account Orders only when items belong to specific category 仅在 WooCommerce 中为特定用户角色启用送货方式 - Enable shipping method for specific user role ONLY in WooCommerce 在 WooCommerce 中添加基于用户角色的自定义我的帐户菜单项 3+ - Add custom my account menu item based on user role in WooCommerce 3+ Woocommerce 为特定用户角色设置最低订单 - Woocommerce set minimum order for a specific user role
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM