Unleashing the Benefits of the WordPress Gutenberg Editor
In December 2018, WordPress introduced a revolutionary update to its content creation experience—the Gutenberg editor.
Continue ReadingWooCommerce is one of the most popular plugins in the eCommerce domain. Many theme developers have used the popularity of this plugin to their advantage and have developed WooCommerce compatible themes.
While developing a client site, we realized that the individual product page had only one additional tab apart from the ‘description’ tab and that is the ‘review’ tab. However, the client required around six different tabs on an individual product page. To achieve the required result we used the following method for the customization.
The ‘woocommerce_product_tabs’ filter provided by WooCommerce should be used as below to add a custom tab to a product page in WooCommerce. The code should be added to the functions.php file of your theme.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['desc_tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}
So on using this code, we were able to add an additional tab to the already existing tabs.
Once a custom tab has been added the content for the tab can be added in two ways. The content can be either added as static data or the content can be fetched from the custom fields added to the admin dashboard. The following code should be added to the functions.php file of your theme to add static data to the custom tab.
function woo_new_product_tab_content() {
// The new tab content
echo '<p>Lorem Ipsum</p>';
}
If you have custom fields provided in your admin dashboard for your product, then the following code would be useful to achieve the required result.
function woo_new_product_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<p>'.get_post_meta($prod_id,'additional information',true).'</p>';
}
There can be an instance where the data from the custom tab needs to be added to the cart. For example, if the additional tab is to personalize products then the data specified by the customer would need to be added to the cart.
Custom tabs that have been added can also be rearranged as per the requirement. To achieve this the ‘woocommerce_product_tabs’ filter has to be used once more. The code for the same will be as below.
add_filter( 'woocommerce_product_tabs', 'sb_woo_move_description_tab', 98);
function sb_woo_move_description_tab($tabs) {
$tabs['desc_tab']['priority'] = 5;
$tabs['reviews']['priority'] = 20;
return $tabs;
}
Using the above method, a user can be provided with plenty of content without cluttering the product page. Arranging the data logically not only makes it simpler for the website visitor to understand the features of the product but it also becomes easier for the web admin to maintain the uniformity in data across the product.