Site logo

Archived topic

Change Woo Endpoint Title

3 replies · Started by Samuel on December 13, 2022

Viewing posts 1–4 of 4

Hi,

just found this article: https://generatepress.com/forums/topic/display-woocommerce-endpoint-title-in-header-element/

My question is: Is it possible to implement the whole thing better somehow? Now that GBlocks can also output dynamic content?

My problem is the following: I have an element with the following setting https://app.screencast.com/tPDCTILHMMmyR

At the endpoint /lost-password/ a H1 heading appears twice now.

https://app.screencast.com/jkclmMSv1NhhZ

"Forgot password" should either be in the header. So position 1. or the title should not be H1, but H2.

Hi there,

for reference here is the function Woo uses to display the Endpoint title:

https://github.com/woocommerce/woocommerce/blob/3611d4643791bad87a0d3e6e73e031bb80447417/plugins/woocommerce/includes/wc-page-functions.php#L19

One of its conditional template tags is in_the_loop() which stops it from affecting other elements such as menu items that also use the the_title filter. Which is why it won't work with a Page Hero that is hooked in outside the loop.

So one option would be to repurpose the woo function in a render_block filter.

1. The Page Hero would have to be specific to the Account page.
1.1 The H1 Headline should contain a static title string ( not dynamic ) eg. My Account.
1.2 In Advanced > Additional CSS Class(es) add a class eg. endpoint-title

2. Add this PHP Snippet and update the $string and $class variables to match 1.1 and 1.2:


add_filter( 'render_block', function( $block_content, $block ) {
    global $wp_query;

    if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && is_page() && is_wc_endpoint_url() ) {
        
        $endpoint       = WC()->query->get_current_endpoint();
        $action         = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
        $endpoint_title = WC()->query->get_endpoint_title( $endpoint, $action );
        $title          = $endpoint_title ? $endpoint_title : $title;
        $string         = 'My Account'; // String to replace
        $class          = 'endpoint-title'; // block class to target

        if ( ! empty( $block['attrs']['className'] ) && $class === $block['attrs']['className'] ) {
            $block_content = str_replace( $string , $title , $block_content );
        }
    }

    return $block_content;
}, 10, 2 );

Thanks David!

You're welcome

This archived topic is closed to new replies.