Change Product Tab Titles and Headings (ok)

https://rudrastyh.com/woocommerce/rename-product-tabs-and-heading.html

C:\xampp\htdocs\jp1\wp-content\themes\muum-child\functions.php

add_filter( 'woocommerce_product_description_heading', 'misha_description_heading' );
function misha_description_heading( $heading ){
  return '商品概要';
}
add_filter( 'woocommerce_product_additional_information_heading', 'misha_additional_information_heading' );
function misha_additional_information_heading( $heading ){
  return '商品詳細';
}

Change Product Tab Titles and Headings

Change Product Tab Titles

Let’s begin with the simple thing, luckily you can rename any of product tab titles, just by changing the title element of the $tabs array, example:

add_filter( 'woocommerce_product_tabs', 'misha_rename_additional_info_tab' );
 
function misha_rename_additional_info_tab( $tabs ) {
 
	$tabs['additional_information']['title'] = 'Product size';
 
	return $tabs;
 
}

All the code from this tutorial you can insert to your functions.php file, but please keep in mind that unless you are using your custom theme, it is better to insert the code into a child theme’s file or to a custom plugin.

By the way, if you do not need some of the default WooCommerce product tabs, you can check my another tutorial about removing them.

Rename Reviews Tab

But it doesn’t work out the same way for the “Reviews” tab. Why? Because this tab title displays the number of customer reviews as well!

Okay, there are two different approaches for this, in the first one we can just str_replace() the text “Reviews” and do not touch the count.

add_filter( 'woocommerce_product_tabs', 'misha_rename_reviews_tab' );
 
function misha_rename_reviews_tab( $tabs ) {
 
	$tabs['reviews']['title'] = str_replace( 'Reviews', 'What customers are saying', $tabs['reviews']['title'] );
 
	return $tabs;
 
}

In the second approach we can get the reviews count from the global $product object:

add_filter( 'woocommerce_product_tabs', 'misha_rename_reviews_tab' );
 
function misha_rename_reviews_tab( $tabs ) {
 
	global $product;
 
	$tabs['reviews']['title'] = 'What customers are saying (' . $product->get_review_count() . ') ';
 
	return $tabs;
 
}

Change Product Tab Headings

Ok, we’ve figured it out how to rename tabs, but it is not a complete solution without changing the tab headings as well 🤔

Each tab has a different filter hook which allows to change its heading, let’s make a look at them separately.

Change “Description” tab heading

add_filter( 'woocommerce_product_description_heading', 'misha_description_heading' );
function misha_description_heading( $heading ){
 
	return 'My new heading';
 
}

Change “Additional information” tab heading

Everything is very similar to Description tab. Actually we have just the same code but with the different filter hook.

add_filter( 'woocommerce_product_additional_information_heading', 'misha_additional_info_heading' );
function misha_additional_info_heading( $heading ){
 
	return 'My new heading';
 
}

Change “Reviews” heading

Here is where the interesting things come in. Yes, we have a filter hook here… it is woocommerce_reviews_title but.. it only works in case you have reviews! So, if a product has no customer reviews, the hook won’t work!

Of course I will show you a solution, but let’s make a look on an example in case there are reviews:

add_filter( 'woocommerce_reviews_title', 'misha_reviews_heading', 10, 3 );
function misha_reviews_heading( $heading, $count, $product ){
 
	return 'What customers think about this product';
 
}

As you can see, it is not necessary to get the reviews count from global $product as we did it before, everything is already provided within filter hook arguments, you can even access a WC_Product object inside it.

And now shocking news – WooCommerce doesn’t have a hook for “Reviews” tab heading when there are no reviews! 😱

But I promised you a solution, right? 🔥👇

add_filter( 'gettext', 'misha_no_reviews_heading', 20, 3 );
function misha_no_reviews_heading( $translated, $text, $domain ) {
 
	if( function_exists( 'is_product' ) && is_product() && $translated == 'Reviews' && $domain == 'woocommerce' ) {
		$translated = 'Ooops... No reviews yet. Please leave one!';
	}
 
	return $translated;
 
}

Is it a clean solution to hook the translation? Kind of clean, if you’re absolutely sure that you have to rename this, you can use it, but please do not forget about all the conditional statements to prevent it from translating somewhere else you do not need.

More about Product Tabs

Last updated