The category page on WooCommerce is in many ways like an aisle or a section of a super-market, where you can buy goods that fall under a particular category.
A basic WooCommerce Category page includes the Title (Category Name), description of the category (if added), a sorting filter, followed by the product listing. In this post, you will learn how to add text to the category description in WooCommerce.
This is how a product Category page looks like:
The category description can be changed via your WordPress dashboard by clicking on Products->Categories and then clicking “Edit” below the name of the Category you want to edit.
Adding text below the category description
Sometimes, you may want to announce something or put up a notification, which is applicable to all categories or across the store. A good way to do this is to add this notification to the category description. In this case, it is not feasible to edit descriptions of all categories (especially when there are many categories).
It will thus be useful to learn how to add text to the category description in WooCommerce using code snippets, such that this text will show up with the descriptions of all categories.
Insert the below code snippet in the functions.php file of your child theme. (To know why we edit the functions.php of the child theme and not directly the functions.php file, read this post.)
add_action( 'woocommerce_archive_description', 'ts_add_to_category_description' ); function ts_add_to_category_description() { if ( is_product_category()) { echo 'Buy on or before 28th February to avail 40% OFF on ALL products across the store!<br/>Discount will be applied at checkout<br/><br/>'; } }
Here, woocommerce_archive_description is a filter used for all archive pages in WooCommerce and is_product_category() checks whether the current page is a category archive page. You will see that the text is now visible below your category description:
With the same code snippet, you can also format the text using HTML tags and CSS styles.
add_action( 'woocommerce_archive_description', 'ts_add_to_category_description' ); function ts_add_to_category_description() { if ( is_product_category()) { echo '<span style="color:#003366;"><b>Buy on or before 28th February to avail 40% OFF on ALL products across the store!<br/>Discount will be applied at checkout</b></span><br/><br/>'; } }
Adding text above the category description
Sometimes, you may come across a use-case where you need to display text above the Category description. The code snippet for this will be different because this will involve removing the category description from showing on the page, displaying the text that you want to, and then printing the category description.
Have a look at the code snippet below:
remove_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 ); remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 ); add_action( 'woocommerce_archive_description', 'ts_add_to_category_description' ); function ts_add_to_category_description() { if ( is_product_category()) { $cat_desc = term_description( $cat_id, 'product_cat' ); echo '<span style="color:#003366;"><b>Buy on or before 28th February to avail 40% OFF on ALL products across the store!<br/>Discount will be applied at checkout</b></span><br/><br/>'.$cat_desc; } }
The first two lines in this code are used for unhooking the functions that display the category descriptions. term_description() is a function that returns the category description. It takes two arguments, one is the category id and the other is the name of the taxonomy. In our case, $cat_id has been predefined globally. The name of the taxonomy is “product_cat” which mentions that this is a product category. The category description ($cat_desc) that is returned by the function term_description(), is appended to the custom text that you want to display. Thus, we will first see the custom text & then the description that is already saved in the Categories section.
You can see that the category description is visible below our custom text:
In this way, you can add text above or below the product category description and display it on the category page.
Whether it is a store-wide sale, a free shipping (above a certain price) notification, a notification about change in pricing, introduction of new taxes, or a new policy that you want the customer to take note of, this feature is handy in all such cases.
I would like to add additional custom text to only one specific category description.
Hey Dave!
You can use the “SAMPLE TEXT” on a specific category using Product Description. When you put the SAMPLE TEXT in “DESCRIPTION”, then it is shown on a specific category on the front-end.
Thanks
YASHPAL KUMAR
NOIDA-Delhi
Can you please explain a little bit better? Thanks
Thanks, Worked for me! (although I used it only to output the $cat_desc as my theme was doing it by default for whatever reason 🙂
Hi there, how do i do this so the description shows up on page 2, 3 of category pages? right now the above only works with the first page.. thank you.
I would like to add under products,
Thanks
Hello, this does not work on new woocommerce – 4.8.0, Please update the article
i would like to show it under below the products!