WooCommerce: Hide “View Cart” Button Upon Ajax Add to Cart

WooCommerce has a built-in AJAX functionality for adding products to the cart on archive pages (Shop page, product category pages, tag pages, etc.). Once you add a product, WooCommerce displays a “View Cart” button beside the “Add to Cart” one.

The “View Cart” button is a way to improve user experience, so that customers can quickly access their cart after adding a product. However, I’ve noticed on some client websites that this can mess up the product grid layout, confuse users and – sometimes – hurt the overall UX instead of improving it!

With the snippet below you’ll learn how to remove this button – surely you’re familiar with the CSS display:none method, so we will see a different approach here, so that the button doesn’t even load. Enjoy!

Here’s the (annoying?) “View Cart” button that appears beside the “Add to cart” button once you add the product to the cart and “Ajax add to cart” is enabled in the WooCommerce settings. Let’s get rid of that!

PHP+JS Snippet: Remove “View Cart” Button When Product is Added to Cart Via Ajax @ Shop, Cat, Archive Pages

This code snippet aims to remove the “View Cart” button that typically appears after an AJAX add to cart event in WooCommerce.

Code breakdown:

  1. Hooking the Function
    • The first line add_action( 'wp_footer', 'bbloomer_no_ajax_view_cart_button' ); utilizes the WordPress add_action function.
    • This function hooks the bbloomer_no_ajax_view_cart_button function to the wp_footer action.
    • The wp_footer action signifies that the bbloomer_no_ajax_view_cart_button function will be executed on every WordPress frontend page/post.
  2. bbloomer_no_ajax_view_cart_button
    • This function is designed to specifically target the button that appears after an AJAX add to cart event in WooCommerce.
  3. wc_enqueue_js
    • The line wc_enqueue_js is a WooCommerce function used to enqueue (include) JavaScript code.
    • The JavaScript code provided within the quotation marks is the core functionality of this code snippet.
  4. JavaScript Code
    • This code snippet utilizes jQuery to target the DOM (Document Object Model) after the document body is loaded ($( document.body ).on('wc_cart_button_updated', function(){...})).
    • It specifically looks for the element with the class added_to_cart.wc-forward which is the “View Cart” button that appears after an AJAX add to cart.
    • When the wc_cart_button_updated event is detected (indicating an AJAX add to cart has happened), the JavaScript code removes the targeted element using the .remove() function.
/**
 * @snippet       Hide "View Cart" @ Woo Shop Page
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_action( 'wp_footer', 'bbloomer_no_ajax_view_cart_button' );
	
function bbloomer_no_ajax_view_cart_button() {
	wc_enqueue_js( "
		$( document.body ).on('wc_cart_button_updated', function(){
			$('.added_to_cart.wc-forward').remove();
		});	
	" );
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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