在 WooCommerce 中设置我的帐户自定义项目端点标题

C_C*_*see 4 php wordpress endpoint woocommerce hook-woocommerce

我通过成功添加新端点来自定义我的 woocommerce 帐户页面。\n这个新端点的标题是“我的帐户”默认标题。我也想自定义标题。\n我尝试使用挂钩“woocommerce_endpoint_{endpoints}_title”,它在默认端点上完美工作,但它似乎不适用于自定义端点:

\n

我的自定义端点(不起作用):

\n
add_filter( \'woocommerce_endpoint_favoris_title\', \'change_my_account_favoris_title\', 10, 2 );\nfunction change_my_account_favoris_title( $title, $endpoint ) {\n    $title = __( "Tests", "woocommerce" );\n    return $title;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

自定义端点

\n

默认端点示例工作:

\n
add_filter( \'woocommerce_endpoint_edit-account_title\', \'change_my_account_edit_title\', 10, 2 );\nfunction change_my_account_edit_title( $title, $endpoint ) {\n    $title = __( "Tests", "woocommerce" );\n    return $title;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

默认端点

\n

目前找不到任何相关内容,\n感谢您的宝贵时间

\n
\n

编辑:

\n

我的 woocommerce 帐户部分的完整挂钩:

\n

1/ 创建端点:

\n
add_filter (\'woocommerce_account_menu_items\', \'custom_log_history_link\', 40);\nfunction custom_log_history_link($menu_links){\n    $menu_links = array_slice( $menu_links, 0, 5, true )\n        + array( \'favoris\' => \'Mes favoris\' )\n        + array_slice( $menu_links, 5, NULL, true )\n        + array( \'delete-account\' => \'Supprimer mon compte\' )\n        + array_slice( $menu_links, 5, NULL, true );\n    return $menu_links;\n}\nadd_action( \'init\', \'custom_add_endpoint\' );\nfunction custom_add_endpoint() {\n    add_rewrite_endpoint( \'favoris\', EP_PAGES );\n    add_rewrite_endpoint( \'delete-account\', EP_PAGES);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

2/ 新端点的内容:

\n
add_action( \'woocommerce_account_favoris_endpoint\', \'custom_my_account_endpoint_content_favoris\' );\nfunction custom_my_account_endpoint_content_favoris() {\n    $user = wp_get_current_user();\n    $user_id=$user->ID;\n    $favoris=get_field(\'liste_des_favoris\',\'user_\'.$user_id);\n    $favoris_id=array();\n    echo "<h3>Vos produits favoris :</h3>";\n    if ($favoris!=\'\'){\n        foreach ($favoris as $favori) {\n            $favoris_id[] = $favori[\'produit_favori\'];\n        }\n        echo \'<ul class="list-produits">\';\n        foreach($favoris as $favori){\n            $product=wc_get_product($favori[\'produit_favori\']);\n            $product = $product->get_data();\n            $sidebar = true;\n            ItemProduit($product,$sidebar,$favoris_id,$user_id);\n        }\n        echo \'</ul>\';\n    } else {\n        echo "<p>Vous n\'avez pas de favoris pour le moment1.</p>";\n    }\n}\nadd_action( \'woocommerce_account_delete-account_endpoint\', \'custom_my_account_endpoint_content\' );\nfunction custom_my_account_endpoint_content() {\n    echo "<p>Etes-vous s\xc3\xbbr de vouloir supprimer d\xc3\xa9fintivement votre compte ?<br/>Attention ! Toute suppression de compte est d\xc3\xa9finitive, vous perdrez tout l\'historique de vos achats.<br/>En supprimant votre compte, toutes vos donn\xc3\xa9es personnelles seront d\xc3\xa9finitivement effac\xc3\xa9es de notre base de donn\xc3\xa9es.</p>";\n    echo \'<p class="status"></p>\';\n    wp_nonce_field( \'ajax-delete-nonce\', \'delete-security\' );\n    echo \'<div class="btns-delete">\';\n    echo \'<div class="btn btn-red" id="submit-delete">Supprimer</div>\';\n    echo \'</div>\';\n}\n
Run Code Online (Sandbox Code Playgroud)\n

3/ 自定义菜单顺序:

\n
add_filter ( \'woocommerce_account_menu_items\', \'custom_sort_menu\' );\nfunction custom_sort_menu( $menu_links ){\n    $menu_links = array(\n        \'dashboard\' => \'Tableau de bord\',\n        \'orders\' => \'Mes commandes\',\n        \'favoris\' => \'Mes favoris\',\n        \'edit-address\' => \'Mes adresses\',\n        \'edit-account\' => \'D\xc3\xa9tails du compte\',\n        \'customer-logout\' => \'D\xc3\xa9connexion\',\n        \'delete-account\' => \'Supprimer mon compte\',\n    );\n    return $menu_links;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Loi*_*tec 9

为了允许复合过滤器挂钩woocommerce_endpoint_{$endpoint}_title与自定义“我的帐户”端点一起使用,您的代码中缺少一些内容。您需要将这些端点声明为woocommerce_get_query_vars过滤器挂钩中的查询变量,如下所示:

\n
add_filter( \'woocommerce_get_query_vars\', \'myaccount_custom_endpoints_query_vars\' );\nfunction myaccount_custom_endpoints_query_vars( $query_vars ) {\n    $query_vars[\'favoris\'] = \'favoris\';\n    $query_vars[\'delete-account\'] = \'delete-account\';\n\n    return $query_vars;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然后,要更改您可以有效使用的自定义端点标题:

\n
add_filter( \'woocommerce_endpoint_favoris_title\', \'change_my_account_favoris_title\' );\nfunction change_my_account_favoris_title( $title ) {\n    return __( "Favoris", "woocommerce" );\n}\n\nadd_filter( \'woocommerce_endpoint_delete-account_title\', \'change_my_account_delete_account_title\' );\nfunction change_my_account_delete_account_title( $title ) {\n    return __( "Supprimer mon compte", "woocommerce" );\n}\n
Run Code Online (Sandbox Code Playgroud)\n

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

\n
\n

请记住通过保存对 WordPress 设置“永久链接”部分的更改来刷新重写规则。

\n
\n
\n

其他注意事项:

\n

在您的实际代码中,您在 2 个函数\xe2\x80\xa6 中使用了 2 次钩子,woocommerce_account_menu_items您只需要其中之一

\n