How to Create a Custom Product Tab on Product page

Add custom product tabs in WooCommerce

This post is regarding how we can add custom tabs on a product page on the front-end.
These tabs are additional to the default ‘Description’, ‘Reviews’ and ‘Additional information’ tabs that are available by default.

The default tabs in WooCommerce have the following priorities:

  • Description 10
  • Additional information 20
  • Reviews 30

For adjusting your product in between them we need to give priorities in between.

How to Change the default tab position

function ChangeTabPosition( $tabs ) { 
  if ( isset( $tabs['description'] ) ) {
    $tabs['description']['priority'] = 29;
  }
  if ( isset( $tabs['additional_information'] ) ) {
    $tabs['additional_information']['priority'] = 10;
  }
 return $tabs;
}
add_filter('woocommerce_product_tabs','ChangeTabPosition' );

 


For Adding Product Tab on Product Page we have to define tab and it’s callback in woocommerce hook 'woocommerce_product_tabs'.

The ‘woocommerce_product_tabs’ filter provided by WooCommerce should be used for adding a custom tab to a single product page in WooCommerce. The code should be added to the functions.php file of your theme.

Example :

Before

function AddNewTab( $tabs ) { 
  $tabs['my_new_tab'] = array(
    'title'    => 'New Tab',
    'callback' => 'new_custom_tab_content',
    'priority' => 51,
  );
return $tabs;
}
add_filter('woocommerce_product_tabs','AddNewTab' );
function new_custom_tab_content($slug,$tab) {
  echo '<h3>' . $tab['title'] . '</h3><p>Add Your Content.</p>';
}

 

 

After

We can also do this For a Specific Product Or For Specific Product Type.

Example : For Specific Product

function  AddNewTab( $tabs ) { 
  global $product; 
    if( $product->get_id() == 786 ) {
    $tabs['my_new_tab'] = array(
      'title'    => 'New Tab',
      'callback' => 'new_custom_tab_content',
      'priority' => 51,
    );
  }
return $tabs;
}
add_filter('woocommerce_product_tabs','AddNewTab');
function new_custom_tab_content($slug,$tab) {
  echo '<h3>' . $tab['title'] . '</h3><p>Add Your Content.</p>';
}

 

Example : For Specific Product Type

function AddNewTab( $tabs ) { 
  global $product; 
    if( $product->is_type( 'variable' ) ) {
    $tabs['my_new_tab'] = array(
      'title'    => 'New Tab',
      'callback' => 'new_custom_tab_content',
      'priority' => 51,
    );
  }
return $tabs;
}
add_filter('woocommerce_product_tabs','AddNewTab');
function new_custom_tab_content($slug,$tab) {
  echo '<h3>' . $tab['title'] . '</h3><p>Add Your Content.</p>';
}

 

Categories

Related Blogs

How To add custom product data tabs in WooCommerce

To add custom product data tabs in WooCommerce, you can use the woocommerce_product_data_tabs and woocommerce_product_data_panels hooks. These hooks allow you to add new tabs to the product edit page in the WooCommerce admin area. Here’s a step-by-step guide on how to achieve this:

How to Remove Product Tabs on Product Page

To remove product tabs on the WooCommerce product page, you can use the `woocommerce_product_tabs` filter. This filter allows you to modify or remove the default product tabs provided by WooCommerce. By removing the tabs you don’t want, you can customize the product page layout to suit your needs.

Sort A ACF Repeater Field Results

Filters the field $value after being loaded. Sort a repeater field using a function called array_multisort(). Sorting Filtering Modifying ACF results .

Convert PHP Array into JavaScript Array.

To convert both a normal PHP array, a multidimensional PHP array, and a nested PHP array into JavaScript arrays, you can use JSON encoding and decoding as previously explained. JSON is well-suited for handling various data structures, including both simple arrays and complex nested arrays.