Here's a complete solution that changes the WooCommerce "Add to cart" button to "Go to cart" without page reload, with special handling for variable products:
jQuery(document).ready(function($) {
// Function to update button text and behavior
function updateAddToCartButton(button, inCart = false) {
if (inCart) {
button.text('Go to cart');
button.removeClass('add_to_cart_button ajax_add_to_cart');
button.addClass('go-to-cart');
button.attr('href', wc_add_to_cart_params.cart_url);
button.off('click.goToCart').on('click.goToCart', function(e) {
e.preventDefault();
window.location.href = wc_add_to_cart_params.cart_url;
});
} else {
button.text('Add to cart');
button.removeClass('go-to-cart');
button.addClass('add_to_cart_button ajax_add_to_cart');
button.removeAttr('href');
button.off('click.goToCart');
}
}
// Function to check if a product/variation is in cart
function checkProductInCart(productId, variationId = 0) {
let inCart = false;
// Get cart contents via AJAX
$.ajax({
url: wc_add_to_cart_params.ajax_url,
type: 'POST',
async: false,
data: {
action: 'check_product_in_cart',
product_id: productId,
variation_id: variationId
},
success: function(response) {
if (response.success) {
inCart = response.data.in_cart;
}
}
});
return inCart;
}
// Initial check for all add to cart buttons
function initializeButtons() {
$('.add_to_cart_button, .single_add_to_cart_button').each(function() {
const button = $(this);
const productId = button.data('product_id') || button.closest('form').find('[name="product_id"]').val();
if (productId) {
const inCart = checkProductInCart(productId);
updateAddToCartButton(button, inCart);
}
});
}
// Handle successful add to cart
$(document.body).on('added_to_cart', function(e, fragments, cart_hash, button) {
updateAddToCartButton(button, true);
});
// Handle variation changes for variable products
$('.variations_form').on('found_variation', function(e, variation) {
const form = $(this);
const button = form.find('.single_add_to_cart_button');
const productId = form.find('[name="product_id"]').val();
if (variation.variation_id) {
const inCart = checkProductInCart(productId, variation.variation_id);
updateAddToCartButton(button, inCart);
}
});
// Handle variation reset
$('.variations_form').on('reset_data', function() {
const form = $(this);
const button = form.find('.single_add_to_cart_button');
updateAddToCartButton(button, false);
});
// Initialize on page load
initializeButtons();
// Reinitialize after cart updates
$(document.body).on('wc_fragments_refreshed', function() {
initializeButtons();
});
});
You'll also need to add this PHP code to your theme's functions.php file to handle the AJAX request:
// AJAX handler to check if product is in cart
add_action('wp_ajax_check_product_in_cart', 'check_product_in_cart_handler');
add_action('wp_ajax_nopriv_check_product_in_cart', 'check_product_in_cart_handler');
function check_product_in_cart_handler() {
$product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;
$variation_id = isset($_POST['variation_id']) ? intval($_POST['variation_id']) : 0;
if (!$product_id) {
wp_send_json_error();
}
$in_cart = false;
if (WC()->cart) {
foreach (WC()->cart->get_cart() as $cart_item) {
if ($variation_id > 0) {
// Check for specific variation
if ($cart_item['variation_id'] == $variation_id) {
$in_cart = true;
break;
}
} else {
// Check for simple product
if ($cart_item['product_id'] == $product_id && empty($cart_item['variation_id'])) {
$in_cart = true;
break;
}
}
}
}
wp_send_json_success(array('in_cart' => $in_cart));
}
// Enqueue the script
add_action('wp_enqueue_scripts', 'enqueue_cart_button_script');
function enqueue_cart_button_script() {
if (is_shop() || is_product_category() || is_product_tag() || is_product()) {
wp_enqueue_script(
'wc-cart-button-switcher',
get_template_directory_uri() . '/js/cart-button-switcher.js',
array('jquery', 'wc-add-to-cart'),
'1.0.0',
true
);
}
}
To implement this:
- Save the JavaScript code in a file called
cart-button-switcher.js in your theme's js directory
- Add the PHP code to your theme's
functions.php file
- The script will automatically:
- Check if products are already in the cart on page load
- Change "Add to cart" to "Go to cart" when items are added
- For variable products, check each variation individually
- Handle the button click to redirect to cart when showing "Go to cart"
The solution works for both simple products and variable products, checking specific variations for variable products as requested.