【问题标题】:How can i rearrange the menus in My-Account Page of WooCommerce?如何重新排列 WooCommerce 的“我的帐户”页面中的菜单?
【发布时间】:2021-06-15 10:18:24
【问题描述】:

我想知道如何在 WooCommerce 中重新排列“我的帐户”页面上的菜单选项卡。

我使用下面的代码添加了一个名为“Affiliate Dashboard”的新菜单,但我想在“Log Out”菜单之前显示它,以便它出现在“Account Details”和“Log Out”菜单之间。

所以安排会是这样的。

1- 仪表板
2- 订单
3- 优惠券
4- 地址
5- 账户详情
6- 附属仪表板
7- 退出

请看截图。

//First hook that adds the menu item to my-account WooCommerce menu 
 
function affiliate_home_link( $menu_links ){
 
    // we will hook "womanide-forum" later
    $new = array( 'affiliate-home' => 'Affiliate Dashboard' );
 
    // 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, 5, true ) 
    + $new 
    + array_slice( $menu_links, 5, NULL, true );
 
 
    return $menu_links;
 
 
}
add_filter ( 'woocommerce_account_menu_items', 'affiliate_home_link' );

// Second Filter to Redirect the WooCommerce endpoint to custom URL
function affiliate_home_hook_endpoint( $url, $endpoint, $value, $permalink ){
 
    if( $endpoint === 'example-forum' ) {
       
        // This is where you add the custom URL, it could be external like, in this case, we need to go to my profile on the bbpress froum
        // I will use this function (bp_core_get_username( bp_loggedin_user_id() );) to get my profile user id and add it to the URL as shown below 
        
        $url = site_url() .'/affiliate-home/' . bp_core_get_username( bp_loggedin_user_id() );
 
    }
    return $url;
 
}
 
 
add_filter( 'woocommerce_get_endpoint_url', 'forum_example_hook_endpoint', 10, 4 );

我们将不胜感激。

谢谢!

【问题讨论】:

标签: wordpress woocommerce


【解决方案1】:

你可以像这样在同一个函数中重新排列你的 $menu_items:

function affiliate_home_link( $menu_links ){

    // Remove the logout menu item, will re-add later
    $logout_link = $menu_links ['customer-logout'];
    unset( $menu_links ['customer-logout'] );
 
    $menu_links ['affiliate-home'] = __( 'Affiliate Dashboard', 'your-plugin-or-theme-textdomain' );

    // Insert back the logout item.
    $menu_links ['customer-logout'] = $logout_link;
   
    return $menu_links;          
}
add_filter ( 'woocommerce_account_menu_items', 'affiliate_home_link', 10, 1 );

测试工作:https://i.imgur.com/DM1eMWH.png

【讨论】:

  • 添加此代码表示我无法重新声明相同的函数“affiliate_home_link”。那么我应该使用什么功能呢?
  • @HayderAllawi 你用我上面的方法替换了你的affiliate_home_link 函数吗?那是你应该做的!我正在做与您尝试实现的相同的事情:添加 Affiliate Dashboard 按钮并将其放置在 Logout 之前。
  • 我不明白你的意思。代码有问题,因为我无法重新声明相同的函数-->affiliate_home_link。我试图更改功能,但出现同样的错误。
  • "我不能重新声明相同的函数" => 只有当您尝试多次使用确切的函数“名称”(在这种情况下为affiliate_home_link)时才会发生这种情况。
  • 如果您删除了您的affiliate_home_link 函数并改用我在上面发布的那个,则不应发生此重新声明问题。此外,有了上面的那个,你就不再需要你的了。这是否更有意义?
【解决方案2】:

您可以使用自定义过滤器并为每个端点设置优先级,每个端点的默认优先级为零。当我需要为一些自定义项目调整菜单时,这是我的方法。

public function filter_woocommerce_account_menu_items( $items ) {
    // Custom menu item
    $items['quick-order-form'] = 'Quick Order Form';
    
    // Sort based on priority
    uksort( $items, function ( $a, $b ) {
        $priority  = [
            'quick-order-form' => 4,
            'customer-logout'  => 5
        ];

        // Check if priority has been set otherwise set to zero
        $aPriority = $priority[ $a ] ?? 0;
        $bPriority = $priority[ $b ] ?? 0;

        // Equal priorities can stay where they are in the array
        if ( $aPriority == $bPriority ) {
            return 0;
        }

        // Adjust sort based on which endpoint has more priority
        return ( $aPriority < $bPriority ) ? - 1 : 1;
    } );

    return $items;
}
add_filter ( 'woocommerce_account_menu_items', 'filter_woocommerce_account_menu_items' );

【讨论】:

    猜你喜欢
    • 2017-01-10
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2017-09-29
    相关资源
    最近更新 更多