user11481313
user11481313

Reputation:

Change Related Products Heading adding the product name in WooCommerce

I have this code which is an attempt in translating "Related products" into "These will go well with PRODUCT NAME".

Here's my code:

add_filter(  'gettext',  'change_related_products_title', 10, 3 );
function change_related_products_title( $translated, $text, $domain  ) {

    $ptitle = get_page_by_title( 'Product Title', OBJECT, 'product' );

    if( $text === 'Related products' && $domain === 'woocommerce' ){
        $translated = esc_html__( 'These go well with '.$ptitle.' ', $domain);
    }
    return $translated;
}

All it shows is "These go well with" and nothing more. Help please.

Upvotes: 2

Views: 2347

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Instead of get_page_by_title() use get_the_title() like:

add_filter(  'gettext',  'change_related_products_title', 10, 3 );
function change_related_products_title( $translated, $text, $domain  ) {
    if( $text === 'Related products' && $domain === 'woocommerce' ){
        $translated = esc_html__( 'These go well with', $domain ) . ' ' . esc_html( get_the_title() );
    }
    return $translated;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Upvotes: 1

Related Questions