WooCommerce prints “Related products” above the related items on every single product page, and that wording rarely matches the voice of the shop. This WordPress snippet changes the WooCommerce related products heading to your own text, without copying a template into your theme and without a translation plugin.
The snippet
<?php
/**
* Replace the "Related products" heading on single product pages.
*/
add_filter( 'woocommerce_product_related_products_heading', 'dpc_related_products_heading' );
function dpc_related_products_heading(): string {
return __( 'Products you may also like', 'dpc' );
}
How it works
- WooCommerce’s
single-product/related.phptemplate passes its heading through thewoocommerce_product_related_products_headingfilter and prints it withesc_html(), so the filter only has to return plain text. - A named function with the
dpc_prefix replaces the anonymous function of the older One-Click Import version, so another snippet can remove it withremove_filter(). __()keeps the new text translatable, which matters on multilingual shops.- The template only prints the
<h2>when the heading is not empty, so returning''hides it.
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty. add_filter() only registers the callback; WooCommerce calls it while it renders the product page. Conditions limited to products would also work, because Conditions code runs on the front end before the template loads, but the filter does nothing on other pages anyway.
The same code also works unchanged in a small plugin or an mu-plugin.
Customise it
- Up-sells (“You may also like…”) use
woocommerce_product_upsells_products_heading; cross-sells in the cart (“You may be interested in…”) usewoocommerce_product_cross_sells_products_heading. Hook a function of the same shape to each. - Different text per category: inside the function, check
has_term( 'tours', 'product_cat' )and return a different string. - Hide the heading: return
''.
Other small WooCommerce text changes in the same style: Woo: Change “Out of Stock” to SOLD OUT and Woo: Change Add to Cart to BUY NOW.
Gotchas
- Block themes: the Single Product template in the Site Editor uses the Related Products block, which has its own Heading block. Edit that heading in the Site Editor; the filter only affects the classic template, which classic themes and the legacy product template use.
- Translation plugins that translate WooCommerce’s own “Related products” string won’t touch your text. Translate the
dpcstring instead. - When a product has no related items, WooCommerce prints nothing at all, heading included.
In the Scripts Organizer library Import it with one click from Scripts Organizer.