These examples show how to use WooCommerce actions and filters to add a message to an order confirmation or classic cart, and change a sale badge in classic product templates.
Note:
This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.
Before you start
↑ Back to topYou need a WooCommerce test site and a way to add PHP to a custom plugin or child theme. Test changes on a staging site first and keep a backup. Do not edit WooCommerce core files. For an introduction, see Hooks, actions and filters.
These examples use fixed English text. Adapt the text and internationalization to your project, and escape any dynamic data before output.
Add a message to the order confirmation
↑ Back to topThe woocommerce_thankyou action receives the order ID. In the classic order confirmation template, it runs when the template renders an order. In a block-based confirmation, it runs through the Additional Information block. This is a page-rendering hook: it can run more than once and does not by itself prove payment succeeded. Do not use it alone for a one-time fulfillment or payment action.
function woo_docs_thankyou_message( $order_id ) {
echo '<p>Thank you for your order.</p>';
}
add_action( 'woocommerce_thankyou', 'woo_docs_thankyou_message' );
To verify the example, place a test order and open its confirmation page. The message should appear where the hook is rendered. If it does not, check that the callback is loaded and, for a block-based confirmation, that the Additional Information block is present.
Add a message before the classic cart
↑ Back to topThe woocommerce_before_cart action runs before the contents of the classic cart template. This example does not target the Cart block.
function woo_docs_cart_message() {
echo '<p>Remember to apply your discount code before checkout.</p>';
}
add_action( 'woocommerce_before_cart', 'woo_docs_cart_message' );
To verify it, open a cart that uses the classic cart template with an item in it. The message should appear above the cart. Check the cart page configuration and any theme template overrides if it does not.
Change the classic sale badge
↑ Back to topThe woocommerce_sale_flash filter changes the sale badge markup in the classic product listing and single-product templates. This example replaces the supplied markup. It does not establish compatibility with product blocks or every theme.
function woo_docs_sale_badge( $html ) {
return '<span class="onsale">Limited Offer</span>';
}
add_filter( 'woocommerce_sale_flash', 'woo_docs_sale_badge' );
To verify it, view a product on sale in a classic product listing or single-product template. The badge should read “Limited Offer.” If it does not, check the active template and theme overrides.
Questions and support
↑ Back to topDo you still have questions and need assistance?
This documentation is about the free, core WooCommerce plugin, for which support is provided in our community forums on WordPress.org. By searching this forum, you’ll often find that your question has been asked and answered before.
If you haven’t created a WordPress.org account to use the forums, here’s how.
- If you’re looking to extend the core functionality shown here, we recommend reviewing available extensions in the WooCommerce Marketplace.
- Need ongoing advanced support or a customization built for WooCommerce? Hire a Woo Agency Partner.
- Are you a developer building your own WooCommerce integration or extension? Check our Developer Resources.
If you weren’t able to find the information you need, please use the feedback thumbs below to let us know.