我正在成功地添加一个新的自定义选项卡到WooCommerce我的帐户部分.但我无法更改菜单项链接,它仍然绑定到‘My-Account’URL.

I want to remove the http://site-test.local/my-account
from this absolute link "http://site-test.local/my-account/https:/mycustomwebsite.com/"

我只是想有"https:/mycustomwebsite.com/"当我点击"新自定义选项卡"菜单链接.

以下是我添加到主题函数.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');   

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

推荐答案

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

// 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_item_key = 'custom_link';
    $menu_items[$menu_item_key] = __('Custom Link', 'woocommerce');

    return $menu_items;
}
   
// 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
        // jQuery code
        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
        // jQuery code
        wc_enqueue_js("$('li.woocommerce-MyAccount-navigation-link--{$menu_item_key} a').attr('href','{$custom_link}').attr('target','_blank');");
    }
}

请注意,使用自定义菜单项链接时,将永远不会显示相关的选项卡内容.

Php相关问答推荐

如何在Livewire中验证多个 Select 字段

将一个数字分配到一系列因素中(就像贪婪算法)

照明\Support\Collection::Filter():参数#1($Callback)的类型必须为?可调用、已给定数组、已调用

在没有谷歌云客户端PHP库的情况下将图像上传到PHP中的Google存储API?

在WooCommerce中添加基于类别的固定费用

PHP空数组定义与开始或结束逗号

在冲突和几何上插入的Postgres参数化查询在PHP中不起作用

是否重新排序多维数组元素以将所有子数组中的子数组移动到元素列表的底部?

Symfony 6.2 IsGraned具有多个角色

PHP根据特定索引值从中删除子数组

Laravel 10中的Validator类

对 WooCommerce 购物车和 checkout 中显示的运输方式进行排序

如何在 Woocommerce 中自动删除旧的已完成订单

PHP Laravel 迁移文件不创建关系

PHP 在数组中搜索值

PHP - 计算非数值数组

防止使用woocommerce_checkout_process下单

Laravel auth()->id() 在生产服务器中不工作

Laravel 查询关系是否为 bool

Laravel Websockets 错误:(WebSocket 在建立连接之前关闭)