Turn WooCommerce product tabs into accordions 2024

With mobile use now more common for shopping online than desktop, it makes sense to customise the shopping experience for mobile shoppers. In this snippet, we will show you how to turn your WooCommerce product tabs into accordions. This will give your shoppers a much better shopping experience than the default nested tab experience.

Currently, when in mobile view the WooCommerce product tabs become responsive but are nested with the tabs at the top and tab area below. This isn’t ideal, especially if you add extra tabs. A significant gap between tab title and content will appear. Turning the tabs into accordions will solve this problem, and enhance the shopping experience greatly. Let’s get started.

/*
 * Snippet: Turn WooCommerce product tabs into accordions 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?824
* Tested with WooCommerce 8.8.2
* "Remove tabs and make new modified tabs"
*/ // removes default woocommerce tabs from single product page remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs' ); // add new modified tabs arranged as accordions add_action( 'woocommerce_after_single_product_summary', 'wcsuccess_custom_tab_template' ); function wcsuccess_custom_tab_template() { $tabs = apply_filters( 'woocommerce_product_tabs', array() ); if ( ! empty( $tabs ) ) : ?> <div class="woocommerce-tabs wc-tabs-wrapper"> <ul class="tabs wc-tabs resp-tabs-list" role="tablist"> <?php // set incremental value to use to check for the first tab which is active on page load $i = 0; // loop through each tab. // Nest the tab content with the tab title foreach ( $tabs as $key => $tab ) : ?> <li class="<?php echo esc_attr( $key ); ?>_tab <?php if($i == 0) echo 'active-tab'; ?>" id="tab-title-<?php echo esc_attr( $key ); ?>" role="tab" aria-controls="tab-<?php echo esc_attr( $key ); ?>"> <a href="#tab-<?php echo esc_attr( $key ); ?>"><?php echo apply_filters( 'woocommerce_product_' . $key . '_tab_title', esc_html( $tab['title'] ), $key ); ?></a> <div class="tab-button <?php if($i == 0) echo 'clicked'; ?>"></div> </li> <div class="resp-tabs-container"> <div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--<?php echo esc_attr( $key ); ?> panel entry-content wc-tab <?php if($i == 0) echo 'active'; ?>" id="tab-<?php echo esc_attr( $key ); ?>" role="tabpanel" aria-labelledby="tab-title-<?php echo esc_attr( $key ); ?>"> <?php call_user_func( $tab['callback'], $key, $tab ); ?> </div> </div> <?php $i++; endforeach; ?> </ul> </div> <?php endif; }

Add the following to the theme functions.php file which will control the JavaScript needed for the accordions.

/*
 * Snippet: Turn WooCommerce product tabs into accordions 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?824
* Tested with WooCommerce 8.8.2
* "Add JS for accordian action"
*/ add_action('wp_footer', 'wc_success_accordion_script'); function wc_success_accordion_script(){ // only load script on product pages if(is_product()){ ?> <script type="text/javascript"> (function($) { // detect click on tab $(document).on('click', '.wc-tabs li', function(){ // get the name of the aria-controls which matches the id of the corresponding tab content let ClickedTab = $(this).attr('aria-controls'); // get th emax scroll height of th ecorresponding tab let maxHeight = document.getElementById(ClickedTab).scrollHeight; // toggle the active class on the tab $(this).toggleClass('active-tab'); // toggle the active class on the tab content $('.wc-tab').toggleClass('active'); // apply css and classes depending on the active-tab class if($(this).hasClass('active-tab')){ $('#'+ClickedTab).css('max-height', maxHeight); $(this).find('.tab-button').addClass('clicked'); } else { $('#'+ClickedTab).css('max-height', '0'); $(this).find('.tab-button').removeClass("clicked"); } }); })( jQuery ); </script> <?php } }

Add the following CSS to your theme customiser CSS section. This will control the styling of the accordions

/*
 * Snippet: Turn WooCommerce product tabs into accordions 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?824
* Tested with WooCommerce 8.8.2
* "Accordian CSS"
*/ .wc-tab { display:block !important; } .wc-tab { max-height:0; overflow:hidden; transition:.5s; } .wc-tab.active { transition:1s; } .wc-tabs li, .wc-tabs li a { width: 100%; } .tab-button { position: absolute; width: 30px; height: 30px; background: #70975B; top: 6px; transform: rotate(0deg); border-radius: 50%; cursor: pointer; z-index: 100; transition: 0.4s cubic-bezier(0.2, 0.6, 0.3, 1.1); right:0 } .tab-button:after { content: ''; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); height: 2px; width: 50%; background: white; } .tab-button:before { content: ''; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); height: 50%; width: 2px; background: white; } .tab-button.clicked { transform: rotate(360deg); background: #CC2A41; } .tab-button.clicked:before { width: 0; }

You should end up with results similar to the below gif. Although the gif isn’t that smooth, the live experience will be much smoother and deliver an improved user experience with cool animations included. You can customise the look and feel further with additional CSS as well.

If you’ve used this on your website, leave a link below to a product page to demonstrate it in action.

5 3 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sophia
Sophia
January 10, 2024 8:00 am

Hi! How can we make this work with elementor (tabs widget)?

David
David
February 24, 2024 8:19 am

Hi, everythink working good but tab button is not visible. Maybe I’am missing something.

Richard
Richard
March 18, 2024 10:14 pm

Great code. But I have two questions. How to force it only in mobile mode. I also have a problem that after expanding and re-collapsing, one line of description is visible (the rest is covered).

4
0
Would love your thoughts, please comment.x
()
x
Scroll to Top
×