简体   繁体   English

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

[英]Set My Account custom items endpoints titles in WooCommerce

I customise my woocommerce account pages by adding new endpoints successfully.我通过成功添加新端点来自定义我的 woocommerce 帐户页面。 The title oh this new endpoints is "My account" default title.这个新端点的标题是“我的帐户”默认标题。 I would like to customize the title as well.我也想自定义标题。 I tried with the hook "woocommerce_endpoint_{endpoints}_title", which works perfectly on defaults endpoints, but it doesn't seem to work on custom endpoint :我尝试使用钩子“woocommerce_endpoint_{endpoints}_title”,它在默认端点上完美运行,但它似乎不适用于自定义端点:

My custom endpoint (not working):我的自定义端点(不工作):

add_filter( 'woocommerce_endpoint_favoris_title', 'change_my_account_favoris_title', 10, 2 );
function change_my_account_favoris_title( $title, $endpoint ) {
    $title = __( "Tests", "woocommerce" );
    return $title;
}

Custom endpoint自定义端点

Default endpooint example working:默认端点示例工作:

add_filter( 'woocommerce_endpoint_edit-account_title', 'change_my_account_edit_title', 10, 2 );
function change_my_account_edit_title( $title, $endpoint ) {
    $title = __( "Tests", "woocommerce" );
    return $title;
}

Default endpoint默认端点

Can't find anything on this for now, Thanks for your time暂时找不到关于此的任何内容,谢谢您的时间


EDIT:编辑:

My complete hooks of my woocommerce account section:我的 woocommerce 帐户部分的完整挂钩:

1/ Creation of endpoints : 1/ 创建端点:

add_filter ('woocommerce_account_menu_items', 'custom_log_history_link', 40);
function custom_log_history_link($menu_links){
    $menu_links = array_slice( $menu_links, 0, 5, true )
        + array( 'favoris' => 'Mes favoris' )
        + array_slice( $menu_links, 5, NULL, true )
        + array( 'delete-account' => 'Supprimer mon compte' )
        + array_slice( $menu_links, 5, NULL, true );
    return $menu_links;
}
add_action( 'init', 'custom_add_endpoint' );
function custom_add_endpoint() {
    add_rewrite_endpoint( 'favoris', EP_PAGES );
    add_rewrite_endpoint( 'delete-account', EP_PAGES);
}

2/ Content of new endpoints: 2/新端点的内容:

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

3/ Custom menu order : 3/自定义菜单顺序:

add_filter ( 'woocommerce_account_menu_items', 'custom_sort_menu' );
function custom_sort_menu( $menu_links ){
    $menu_links = array(
        'dashboard' => 'Tableau de bord',
        'orders' => 'Mes commandes',
        'favoris' => 'Mes favoris',
        'edit-address' => 'Mes adresses',
        'edit-account' => 'Détails du compte',
        'customer-logout' => 'Déconnexion',
        'delete-account' => 'Supprimer mon compte',
    );
    return $menu_links;
}

To allow the composite filter hook woocommerce_endpoint_{$endpoint}_title to work with custom My Account endpoints, there is something missing from your code.为了允许复合过滤器挂钩woocommerce_endpoint_{$endpoint}_title与自定义我的帐户端点一起使用,您的代码中缺少一些内容。 You need to declare those endpoints as query vars in woocommerce_get_query_vars filter hook as follows:您需要在woocommerce_get_query_vars过滤器钩子中将这些端点声明为查询变量,如下所示:

add_filter( 'woocommerce_get_query_vars', 'myaccount_custom_endpoints_query_vars' );
function myaccount_custom_endpoints_query_vars( $query_vars ) {
    $query_vars['favoris'] = 'favoris';
    $query_vars['delete-account'] = 'delete-account';

    return $query_vars;
}

Then to change that custom endpoints titles you can use effectively:然后要更改您可以有效使用的自定义端点标题:

add_filter( 'woocommerce_endpoint_favoris_title', 'change_my_account_favoris_title' );
function change_my_account_favoris_title( $title ) {
    return __( "Favoris", "woocommerce" );
}

add_filter( 'woocommerce_endpoint_delete-account_title', 'change_my_account_delete_account_title' );
function change_my_account_delete_account_title( $title ) {
    return __( "Supprimer mon compte", "woocommerce" );
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

Remember to refresh rewrite rules by saving changes on Wordpress settings "Permalinks" section.请记住通过保存对 Wordpress 设置“永久链接”部分的更改来刷新重写规则。


Other notes:其他注意事项:

In your actual code, you are using 2 times the hook woocommerce_account_menu_items in 2 functions… You need just one of them在您的实际代码中,您在 2 个函数中使用了woocommerce_account_menu_items钩子的 2 倍……您只需要其中一个

One way of changing the title of the custom WooCommerce endpoints is to use the_title filter with the in_the_loop conditional更改自定义 WooCommerce 端点标题的一种方法是将the_title过滤器与in_the_loop条件一起使用

Here is an example that you'll need to modify per your own requirements.这是您需要根据自己的要求修改的示例。

function wpb_woo_endpoint_title( $title, $id ) {

    if ( is_wc_endpoint_url( 'downloads' ) && in_the_loop() ) { // add your endpoint urls
        $title = "Download MP3s"; // change your entry-title
    }

    elseif ( is_wc_endpoint_url( 'orders' ) && in_the_loop() ) {
        $title = "My Orders";
    }

    elseif ( is_wc_endpoint_url( 'edit-account' ) && in_the_loop() ) {
        $title = "Change My Details";
    }
    return $title;
}

add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM