简体   繁体   中英

Set My Account custom items endpoints titles in WooCommerce

I customise my woocommerce account pages by adding new endpoints successfully. 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 :

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:

1/ Creation of endpoints :

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:

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 :

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. You need to declare those endpoints as query vars in woocommerce_get_query_vars filter hook as follows:

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). Tested and works.

Remember to refresh rewrite rules by saving changes on Wordpress settings "Permalinks" section.


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

One way of changing the title of the custom WooCommerce endpoints is to use the_title filter with the in_the_loop conditional

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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