Below is a compact “drop-in” solution that does three things:
-
On every page-load it scans the DOM and immediately changes the Add to cart buttons of the products / variations that are already in the cart to Go to cart.
-
When a customer presses an AJAX Add to cart button the label of that button is switched to Go to cart the very moment WooCommerce finishes the AJAX request – no page refresh.
-
For variable products it only changes the button of the exact variation that is (or becomes) part of the cart – not the whole product.
➊ PHP – enqueue & localise
Add this to functions.php (or a small plugin):
/**
* Enqueue Go-to-Cart helper
*/
add_action( 'wp_enqueue_scripts', 'my_enqueue_go_to_cart_script', 20 );
function my_enqueue_go_to_cart_script() {
// 1. Collect the product & variation IDs that are in the current cart
$product_ids = array();
$variation_ids = array();
if ( WC()->cart && ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $item ) {
$product_ids[] = (int) $item['product_id'];
if ( ! empty( $item['variation_id'] ) ) {
$variation_ids[] = (int) $item['variation_id'];
}
}
}
// 2. Enqueue our small JS helper (after WC scripts)
wp_enqueue_script(
'my-go-to-cart',
get_stylesheet_directory_uri() . '/js/go-to-cart.js',
array( 'jquery', 'woocommerce' ),
'1.0',
true
);
// 3. Pass data from PHP ➜ JS
wp_localize_script(
'my-go-to-cart',
'wcGoToCart',
array(
'cart_url' => wc_get_cart_url(),
'product_ids' => array_values( array_unique( $product_ids ) ),
'variation_ids' => array_values( array_unique( $variation_ids ) ),
'i18n_add_to_cart'=> __( 'Add to cart', 'woocommerce' ),
'i18n_go_to_cart' => __( 'Go to cart', 'woocommerce' ),
)
);
}
➋ JS – logic that does the magic ( /js/go-to-cart.js )
jQuery( function ( $ ) {
/* ----------------------------------------------------------
* tiny helper that morphs any WC “add to cart” button
* into a “go to cart” button / link
* -------------------------------------------------------- */
function makeGoToCart( $btn ) {
$btn.removeClass( 'ajax_add_to_cart add_to_cart_button loading' )
.addClass( 'go_to_cart_button' )
.text( wcGoToCart.i18n_go_to_cart );
// <a> elements
if ( $btn.is( 'a' ) ) {
$btn.attr( 'href', wcGoToCart.cart_url );
}
// <button> elements (mainly on single product pages)
else {
$btn.attr( 'type', 'button' ).off( 'click' ).on( 'click', function () {
window.location.href = wcGoToCart.cart_url;
} );
}
}
/* ----------------------------------------------------------
* 1. Initial scan → buttons of items already in the cart
* -------------------------------------------------------- */
$( 'a.ajax_add_to_cart, button.single_add_to_cart_button' ).each( function () {
let $btn = $( this ),
pid = parseInt( $btn.data( 'product_id' ) || 0 ),
vid = parseInt( $btn.data( 'variation_id' ) || 0 );
if ( wcGoToCart.product_ids.includes( pid ) &&
( vid === 0 || wcGoToCart.variation_ids.includes( vid ) ) ) {
makeGoToCart( $btn );
}
} );
/* ----------------------------------------------------------
* 2. Right after an AJAX add-to-cart completes
* -------------------------------------------------------- */
$( document.body ).on( 'added_to_cart', function ( e, fragments, cart_hash, $btn ) {
if ( ! $btn || ! $btn.length ) { return; }
let pid = parseInt( $btn.data( 'product_id' ) || 0 ),
vid = parseInt( $btn.data( 'variation_id' ) || 0 );
// keep the JS cache in sync with the cart
if ( pid && ! wcGoToCart.product_ids.includes( pid ) ) {
wcGoToCart.product_ids.push( pid );
}
if ( vid && ! wcGoToCart.variation_ids.includes( vid ) ) {
wcGoToCart.variation_ids.push( vid );
}
makeGoToCart( $btn );
} );
/* ----------------------------------------------------------
* 3. Variable product → switch button depending on
* the currently selected variation
* -------------------------------------------------------- */
$( 'form.variations_form' ).on( 'found_variation', function ( e, variation ) {
let $btn = $( this ).find( 'button.single_add_to_cart_button' );
if ( ! $btn.length ) { return; }
if ( wcGoToCart.variation_ids.includes( variation.variation_id ) ) {
makeGoToCart( $btn );
} else {
// reset to “add to cart” in case customer changes variation
$btn.removeClass( 'go_to_cart_button' )
.addClass( 'single_add_to_cart_button' )
.attr( 'type', 'submit' )
.text( wcGoToCart.i18n_add_to_cart );
}
} );
} );
How it works
• PHP side collects the IDs that are inside the cart right now and sends them to the browser together with the cart URL and translated strings.
• JavaScript transforms the button(s)
– On load for already-in-cart items
– On the added_to_cart AJAX event for freshly added items
– On every variation switch (found_variation) so that only the selected variation’s button is affected.
That’s it – the customer sees the button flip to Go to cart instantly, and only for the items that really are in the cart, no page refresh involved. 🎉