WooCommerce offers a range of built-in shortcodes to display products, categories, carts, and more across your website. While these shortcodes are powerful, you may want to modify their behaviour to better fit your store’s needs. In this guide, we’ll explore how you can edit WooCommerce shortcodes without using a plugin by overriding them in your theme or creating new ones.
As always, we recommend adding any custom code to a child theme to ensure your changes persist through theme updates.
Why Edit WooCommerce Shortcodes?
Customising WooCommerce shortcodes gives you control over how products and content are displayed. Here are some key reasons to edit them:
- Change Layouts: Modify how product grids or lists are displayed.
- Personalise Customer Experience: Add custom elements like badges or buttons within shortcode outputs.
- Create Custom Product Displays: Tailor shortcodes to show specific products or categories dynamically.
Step 1: Overriding WooCommerce Shortcodes in Your Theme
You can override WooCommerce shortcodes by creating custom functions in your theme’s functions.php file.
Example: Modifying the [products] Shortcode
Here’s how to modify the [products] shortcode to display a badge on discounted products.
/*
* Snippet: How to Edit WooCommerce Shortcodes – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1587
* Tested with WooCommerce 10.7.0
* "This function overrides the WooCommerce products shortcode to add discount badges"
*/
function wcsuccess_custom_products_shortcode( $atts ) {
$atts = shortcode_atts( array(
'columns' => '4',
'orderby' => 'date',
'order' => 'desc'
), $atts, 'products' );
ob_start();
$query_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => $atts['orderby'],
'order' => $atts['order'],
);
$products = new WP_Query( $query_args );
if ( $products->have_posts() ) {
echo '<ul class="products columns-' . esc_attr( $atts['columns'] ) . '">';
while ( $products->have_posts() ) {
$products->the_post();
global $product;
echo '<li class="product">';
woocommerce_template_loop_product_link_open();
woocommerce_template_loop_product_thumbnail();
if ( $product->is_on_sale() ) {
echo '<span class="onsale">Sale!</span>'; // Add badge
}
woocommerce_template_loop_product_title();
woocommerce_template_loop_price();
woocommerce_template_loop_add_to_cart();
woocommerce_template_loop_product_link_close();
echo '</li>';
}
echo '</ul>';
}
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( 'products', 'wcsuccess_custom_products_shortcode' );
How This Code Works
- Override
[products]Shortcode: This code replaces the default WooCommerce[products]shortcode output with a customised version. - Discount Badge: It checks if a product is on sale and adds a “Sale!” badge if applicable.
Step 2: Creating a Custom WooCommerce Shortcode
If the built-in WooCommerce shortcodes don’t meet your needs, you can create your own custom shortcodes to display products dynamically.
Here’s how you can create a shortcode to show the latest products:
/*
* Snippet: How to Edit WooCommerce Shortcodes – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1587
* Tested with WooCommerce 10.7.0
* "This function creates a custom shortcode to display latest products"
*/
function wcsuccess_latest_products_shortcode( $atts ) {
$atts = shortcode_atts( array(
'limit' => '5',
), $atts, 'latest_products' );
ob_start();
$query_args = array(
'post_type' => 'product',
'posts_per_page' => $atts['limit'],
'orderby' => 'date',
'order' => 'DESC',
);
$products = new WP_Query( $query_args );
if ( $products->have_posts() ) {
echo '<ul class="latest-products">';
while ( $products->have_posts() ) {
$products->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( 'latest_products', 'wcsuccess_latest_products_shortcode' );
How This Works
- Custom Shortcode: The shortcode
[latest_products limit="5"]displays the latest five products. - Shortcode Flexibility: The
limitattribute can be adjusted to display as many products as needed.
Step 3: Using Shortcodes Dynamically with do_shortcode()
Shortcodes can also be embedded dynamically within templates using the do_shortcode() function.
echo do_shortcode('[latest_products limit="3"]');This code renders the [latest_products] shortcode directly inside any template or PHP file, making it easy to integrate customised product displays across your site.
Best Use Cases for Editing WooCommerce Shortcodes
- Highlight Promotions: Add badges or labels to products on sale.
- Custom Product Displays: Create unique product grids or lists using tailored shortcodes.
- Improved User Experience: Modify existing shortcodes to provide more relevant product information for customers.
Conclusion
Editing WooCommerce shortcodes gives you control over how products and other content are displayed on your site. Whether you’re modifying existing shortcodes or creating your own, these techniques allow you to customise your store without the need for plugins.
Always test your changes on a staging site before applying them live, and be sure to add any custom code to a child theme to protect your changes from theme updates. For further WooCommerce customisations, explore our WooCommerce Visual Hooks Guide and wp-config generator.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

