How to Edit WooCommerce Shortcodes – 2026

Last updated on October 30th, 2024 at 09:54 am

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.
See also  How to Block Bad Buyers in WooCommerce Without a Plugin - 2026

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 limit attribute 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.

See also  How to Split Variable Products Into Simple Products Without a Plugin in WooCommerce - 2026

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.

0 0 votes
Article Rating

Stay In Touch

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

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×