在 WooCommerce 我的帐户部分添加带有自定义链接的自定义菜单项

问题描述 投票:0回答:1

我已成功将新的自定义选项卡添加到 WooCommerce 我的帐户部分。但我无法更改菜单项链接,它仍然与“我的帐户”URL 绑定。

我想删除 http://site-test.local/my-account
从这个绝对链接“http://site-test.local/my-account/https:/mycustomwebsite.com/”

当我单击“新自定义选项卡”菜单链接时,我只想拥有“https:/mycustomwebsite.com/”。

这是我添加到主题functions.php文件中的代码:

function custom_add_my_account_menu_item($items) {
    // Add a new menu item with the key 'custom_option'
    $items['https://mycustomwebsite.com'] = __('NEW CUSTOM TAB', 'your-text-domain');

    $cleanLink = str_replace('http://site-test.local/my-account', '', $items); //I'm trying to remove the site-test.local/my-account, but it doesn't work
    
    $items = $cleanLink;
    
    return $items;
}

add_filter('woocommerce_account_menu_items', 'custom_add_my_account_menu_item');

// Display content for the custom option in the My Account section
function custom_my_account_endpoint_content() {
    echo '<p>This is the content for the custom option.</p>';
}

add_action('woocommerce_account_custom_option_endpoint', 'custom_my_account_endpoint_content');   

如何将菜单项链接更改为自定义链接?

php jquery wordpress woocommerce account
1个回答
0
投票

要设置指向自定义“我的帐户”菜单项的自定义链接,您可以使用以下命令:

// Add a custom menu item
add_filter('woocommerce_account_menu_items', 'add_my_account_custom_menu_item');
function add_my_account_custom_menu_item( $menu_items ) {
    $menu_items['custom_link'] = __('Custom Link', 'woocommerce');

    return $menu_items;
}

// Display the tab content for the 'custom_link' menu item (optional as with an external link it will not be displayed)
add_action('woocommerce_account_custom_option_endpoint', 'custom_my_account_endpoint_content');
function custom_my_account_endpoint_content() {
    echo '<p>This is the content for the Custom Link.</p>';
}
   
// Replace the custom menu item Link
add_action('template_redirect', 'change_my_account_custom_menu_item_link', 10);
function change_my_account_custom_menu_item_link() {
    if ( is_user_logged_in() && is_account_page() ) {
        $menu_item_key = 'custom_link'; // HERE set the custom menu item key you are using
        $custom_link   = esc_url('https://mycustomwebsite.com/'); // HERE set your custom link
        wc_enqueue_js("$('li.woocommerce-MyAccount-navigation-link--{$menu_item_key} a').attr('href','{$custom_link}');");
    }
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。


如果您想在新窗口中打开链接,请将最后一个函数替换为:

// Replace the custom menu item Link
add_action('template_redirect', 'change_my_account_custom_menu_item_link', 10);
function change_my_account_custom_menu_item_link() {
    if ( is_user_logged_in() && is_account_page() ) {
        $menu_item_key = 'custom_link'; // HERE set the custom menu item key you are using
        $custom_link   = esc_url('https://mycustomwebsite.com/'); // HERE set your custom link
        wc_enqueue_js("$('li.woocommerce-MyAccount-navigation-link--{$menu_item_key} a').attr('href','{$custom_link}').attr('target','_blank');");
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.