Search
Close this search box.

How to Display Custom Notices in WooCommerce Cart Page for Specific Products

A custom cart notice is a great way to inform customers about specific products in their cart. It can highlight important information, promotions, or warnings. It’s easy to add, and I’ll show you how.

This method works if you’re using the WooCommerce cart with shortcodes. If you’ve imported a classic theme demo, you’re probably using a shortcode to show the cart. It’s good to check first.

Go to Dashboard > Pages. Click Edit on the cart page. If it looks like the image below, you can follow these steps.

cart

Step 1: Find the Product(s) ID

First, you need to know the product IDs of the items you want to target. You can find the product ID by going to your product list in WooCommerce. Just hover over the product name, and the ID will show.

screenshot 1 1

Step 2: Add the Code

You should add the code snippet to the functions.php file in your child theme.

If you don’t have a child theme, read this guide on How to Create a Child Theme.

Once you have the child theme, follow these steps:

Go to your WordPress dashboard. Click on Appearance > Theme Editor.

Open the functions.php file for your theme.

Copy and paste this code at the end of the file,

// Show a custom cart notice for specific products
add_action('woocommerce_before_cart', 'custom_cart_notice_for_specific_products');

function custom_cart_notice_for_specific_products() {
    // List of product IDs
    $target_product_ids = array(123, 456); // Replace with your product IDs

    foreach (WC()->cart->get_cart() as $cart_item) {
        if (in_array($cart_item['product_id'], $target_product_ids)) {
            wc_print_notice('This is a special message for this product!', 'notice'); // Change this message
            break; // Show the notice once
        }
    }
}

Replace Notes

  • Make sure to replace 123 and 456 with the correct product IDs. If you want to add more products, just put the IDs in the brackets, separated by a comma.
  • Also, change the text This is a special message for this product! in the code to whatever you want.
functions

Step 3: Test the Cart Notice

After adding the code, add the products to your cart. You should see your custom message when you go to the cart page.

cart page

Share

Twitter
Facebook
LinkedIn
Reddit
Pinterest

Related Posts

Toufique Alahi

Hi there! I enjoy writing about WooCommerce and sharing my hands-on experiences to help you customize your store easily. I explore WooCommerce features, tips, and tweaks to improve your store's performance. Stick around for helpful tutorials and insights!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.