In this tutorial, I’ll show you how to add content in product description tab in globally or conditionally in WooCommerce.
There is no default WooCommerce hook for the description tab to play with, but you can use the_content
filter hook, abracadabra! ?♀️
Adding content in the description tab of all products
This code snippet will add custom content in the description tab of all products on your website.
add_filter( 'the_content', 'wpglorify_custom_description_tab' );
function wpglorify_custom_description_tab( $content ){
if( is_product() ) {
$content .= '<p>Your custom text or HTML.</p>';
}
return $content;
}
You can add your desired text or HTML at line 6.
Adding content in the Description tab for specific Category
Let’s assume you are selling t-shirts on your website among other things and all your t-shirts have something in common like clothing material or a size chart.
Now, Instead of adding the same thing in the description tab of all the t-shirts, you can simply use the snippet below where “t-shirts” is the name of the category.
add_filter( 'the_content', 'wpglorify_custom_description_tab' );
function wpglorify_custom_description_tab( $content ){
if( is_product() && has_term( 't-shirts', 'product_cat' ) ) {
$content .= '<p>Your custom text or HTML.</p>';
}
return $content;
}
“t-shirts” is the name of the category in the above code snippet you can replace it according to your needs.
add content with Shortcode
You can also use shortcode to add any kind of content in the description tab. We can simply use do_shortcode function here to add the shortcode. You can replace the [my_shortcode] inline 6 with the shortcode you want to use
add_filter( 'the_content', 'wpglorify_custom_description_tab' );
function wpglorify_custom_description_tab( $content ){
if( is_product() && has_term( 't-shirts', 'product_cat' ) ) {
$content .= do_shortcode("[my_shortcode]");
}
return $content;
}
Keep in mind if your shortcode has double quotation marks(” “) replace it with single quotation marks (‘ ‘) to avoid errors.
Using the Advanced Custom Fields plugin
You can also use Advanced Custom Fields (ACF) plugin to add custom content in the description tab.
add_filter( 'the_content', 'wpglorify_custom_description_tab' );
function wpglorify_custom_description_tab( $content ){
if( is_product() && has_term( 't-shirts', 'product_cat' ) ) {
$content .= get_field('field_name'); //add your own field_name
}
return $content;
}
This is a very basic example of adding content via ACF. Above, in line six add the actual cutom field name