Want to show shoppers a helpful message that changes depending on what they’ve added to their cart? With a simple WooCommerce snippet, you can display relevant notes for specific products, categories, or cart combinations—such as delivery reminders, bundle suggestions, or a quick thank-you.
In this guide, you’ll learn how to create a WooCommerce custom cart message based on cart contents and add it to your store. It’s a practical way to make the shopping experience more personal and share useful information at just the right moment.
Add a WooCommerce Custom Cart Message Based on Cart Contents
Now that you know what a tailored cart note can do, the next step is deciding what should make it appear. A little conditional logic lets you match the message to products, categories, or a spending threshold.
Choose Which Products, Categories, or Cart Totals Trigger a Message
Start with a useful prompt tied to a shopper’s current selections. For instance, you might show care instructions when a particular item is in the cart, suggest a related product for a category, or remind customers about a benefit once their eligible subtotal reaches a set amount.
Before adding code, note the product ID, category slug, and—if needed—your cart-total threshold. Keep the condition specific and the message helpful; a clear delivery reminder is more useful than a vague sales pitch. You could use rules such as:
- Show a setup tip when product ID 123 is in the cart.
- Display a shipping note when any item belongs to the “coffee” category.
- Offer a free-shipping reminder when the cart contents total reaches $75.
Add the PHP Snippet and Customize Its Message
The example below checks those three conditions and displays one notice on the cart page. If more than one rule matches, the product-specific message takes priority, followed by the category message and then the total-based message.
Add the snippet using a code manager such as Code Snippets, or place it in your child theme’s functions.php file. Replace the sample IDs, category slug, threshold, and wording with details from your store.
addaction( 'woocommercebeforecart', 'mycartcontentsmessage' );
function mycartcontentsmessage() {
if ( ! WC()->cart || WC()->cart->isempty() ) {
return;
}
$hasproduct = false;
$hascategory = false;
foreach ( WC()->cart->getcart() as $item ) {
$productid = $item['productid'];
if ( 123 === (int) $productid ) {
$hasproduct = true;
}
if ( hasterm( 'coffee', 'productcat', $productid ) ) {
$hascategory = true;
}
}
if ( $hasproduct ) {
$message = 'Remember to check the setup guide included with this item.';
} elseif ( $hascategory ) {
$message = 'Coffee orders are roasted fresh before dispatch.';
} elseif ( WC()->cart->getcartcontentstotal() >= 75 ) {
$message = 'Your cart qualifies for free shipping.';
} else {
return;
}
wcprintnotice( eschtml( $message ), 'notice' );
}The total check uses the cart contents total, which excludes shipping and tax. Test each condition with the relevant items in your cart, and confirm that the notice appears only where intended.
Show Messages in the Right Place in Your Store
A timely note can help, but its location matters: a delivery reminder may be most useful while someone reviews their cart, while a final reassurance might fit better during checkout. Choose the spot that supports the next step without interrupting it.
Display the Notice on the Cart or Checkout Page
The snippet currently uses woocommercebeforecart, so the message appears above the cart contents on the cart page. To move it to checkout—or show it on both pages—change the hook where the function is attached.
Use the checkout hook below instead of the existing addaction() line. To display the same message in both places, keep both lines and leave the message function unchanged:
addaction( 'woocommercebeforecheckoutform', 'mycartcontentsmessage' );
// To show it on both pages, use both hooks:
addaction( 'woocommercebeforecart', 'mycartcontentsmessage' );
addaction( 'woocommercebeforecheckoutform', 'mycartcontentsmessage' );Pick the placement based on what shoppers need to know at that moment:
- Cart page: Useful for product tips, category-specific details, or reminders that may affect what customers add or remove.
- Checkout page: Better for a concise delivery note, a last-minute eligibility reminder, or information customers should see before placing an order.
If a message should appear only at checkout, use the checkout hook alone. The function’s existing conditions still decide whether it appears, so you don’t need to duplicate the product or category checks.
One detail to check is your store’s page layout. These hooks work with the traditional WooCommerce cart and checkout templates, but a store using Cart or Checkout blocks may not display every legacy hook in the same way. Test the message on your actual cart and checkout pages, including on mobile, and confirm it appears once, in a clear position, without pushing important buttons or totals out of view.
Test and Troubleshoot Your Custom Cart Message
A message that works in one test cart may behave differently when products, discounts, or page layouts change. A few deliberate checks can catch those surprises before shoppers encounter them and help you confirm that each prompt appears at the right time.
Check Cart Conditions, Theme Compatibility, and Message Visibility
Begin by testing each rule on its own, then try combinations. Because the example displays just one notice, a product-specific match takes priority over a category match, and the category match takes priority over the cart-total rule.
- Product rule: Add the specified product and confirm its message appears.
- Category rule: Add a different product from the target category, then try one that is not in that category.
- Total rule: Test a cart below the threshold and one that reaches it. The example checks the contents total, not shipping or tax.
- Empty cart: Remove all items and verify that no custom notice is shown.
If a condition fails, recheck the product ID or category slug and make sure the snippet is active. For variations, the current example checks the cart item’s parent product ID; if your rule needs to distinguish individual variations, the condition may need to check the variation ID instead. Also look for a PHP error caused by a typo or a duplicate function name.
Next, check how the notice behaves with your theme and other store features. A theme, page builder, or plugin can alter cart templates or styling, while caching may make a recently changed snippet seem not to update. Clear relevant caches, refresh the page, and test while logged out as well as on a mobile screen.
Finally, confirm the notice is visible and appears only once. Traditional WooCommerce templates support the hooks used in the example, but Cart and Checkout blocks may handle legacy hooks differently. If the message is missing or misplaced, test the page with your active setup and check for styling that hides notices. Keep any debugging on a staging site where possible, and avoid displaying PHP errors to shoppers.
Make Every Cart Message Useful and Timely
A WooCommerce custom cart message based on cart contents can give shoppers relevant guidance right when they need it. By tying notices to products, categories, or cart totals, you can share practical details without showing the same message to everyone.
Choose a clear, helpful message and place it on the cart or checkout page where it best supports the customer’s next step. Then test your conditions, theme, and page layout to make sure the notice appears as intended—including on mobile and with your store’s cart or checkout setup.
With a little thoughtful customization, a simple snippet can make shopping feel more personal and help customers move toward checkout with confidence.