WooCommerce PHP Beginner Requires WooCommerce

Change the “Added to Your Cart” Message

Rewrite the notice shown after adding a product to the cart, and point its button to checkout instead of the cart.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 12 lines
add_filter( 'wc_add_to_cart_message_html', function ( string $message, array $products ): string {
	$titles = [];
	foreach ( $products as $product_id => $qty ) {
		$titles[] = get_the_title( $product_id );
	}

	return sprintf(
		'<a href="%s" class="button wc-forward">Checkout</a> %s added. Nice choice!',
		esc_url( wc_get_checkout_url() ),
		esc_html( implode( ', ', $titles ) )
	);
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

After a product is added, WooCommerce shows “Product has been added to your cart” with a View cart button. This snippet rewrites the message and sends the button straight to checkout.

How it works

The wc_add_to_cart_message_html filter receives the message HTML and the products just added, as product id => quantity. The snippet builds a new message from the product names and a Checkout button. Product names are escaped before they’re printed.

The button uses the wc-forward class so it’s styled like WooCommerce’s own.

Things to watch

  • The message only appears for non-AJAX adds, such as from the product page. Shop page buttons that add in the background don’t show it.
  • Return an empty string to remove the message completely.

How to test it

Add a product from its product page. The notice should use your wording and its button should go to checkout.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only