Full width home advertisement

IT Sharing

Travel the world

Climb the mountains

Post Page Advertisement [Top]

Change Product Tab Titles

Change Product Tab Titles


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

add_filter( 'woocommerce_product_tabs', 't2ds_rename_additional_info_tab' );
 
function t2ds_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  the theme, it is better to insert the code into a child theme’s file or to a custom plugin.

And the result:


how to rename WooCommerce product tabs

By the way, if you do not need some of the default WooCommerce product tabs, you can check my other 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', 't2ds_rename_reviews_tab' );
 
function t2ds_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 to count from the global $product object:

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

No matter what way you will choose, it should work great:

How to rename WooCommerce product 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 changing its heading, let’s make a look at them separately.

Change the “Description” tab heading

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

Change the “Additional information” tab heading

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

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

Change the “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 at an example in case there are reviews:

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

As you can see, it is not necessary to get the reviews to count from 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 the “Reviews” tab heading when there are no reviews! 

But I promised you a solution, right? 

add_filter( 'gettext', 't2ds_no_reviews_heading', 20, 3 );
function t2ds_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.

In case you do not believe me, here is a screenshot 

Custom heading for Reviews product tab when there are no reviews

Source: rudrastyh

Không có nhận xét nào:

Đăng nhận xét

Bottom Ad [Post Page]