I am facing an issue while trying to change the text of the ‘Proceed to Checkout’ button in WooCommerce. My theme is in Spanish, and it’s a ‘GeneratePress’ template. However, the shopping cart button and the ‘Remove item’ link have remained in English. I have tried several solutions, including translation filters, DOM manipulation with JavaScript, and other methods suggested online, but none have been successful.
I am using WordPress with WooCommerce, and my goal is to change the text of the ‘Proceed to Checkout’ button to ‘Ir a Pagar’ and ‘Remove item’ to ‘Eliminar’ in my online store. I have tried the following solutions as functions within the functions.php file of my theme:
function custom_proceed_to_checkout_text() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('body').on('updated_checkout', function() {
var buttonText = 'Ir a Pagar';
$('.wp-block-woocommerce-proceed-to-checkout-block .wc-forward').text(buttonText);
});
});
</script>
<?php
}
add_action('wp_footer', 'custom_proceed_to_checkout_text');
The previous function uses jQuery, as does the following one:
// Función para cambiar el texto del botón de checkout
function custom_proceed_to_checkout_text() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$(document.body).on('updated_cart_totals', function () {
var buttonText = 'Ir a Pagar';
$('.wp-block-woocommerce-proceed-to-checkout-block').html('<a class="checkout-button button alt wc-forward" href="' + wc_checkout_params.checkout_url + '">' + buttonText + '</a>');
});
});
</script>
<?php
}
add_action('wp_footer', 'custom_proceed_to_checkout_text');
Neither of the two worked; then I tried with the following:
function custom_proceed_to_checkout_text() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.wp-block-woocommerce-proceed-to-checkout-block').find('.wc-forward').text('Ir a Pagar');
});
</script>
<?php
}
add_action('wp_footer', 'custom_proceed_to_checkout_text');
And with this other one:
function custom_translate_text($translated, $text, $domain) {
if ($text === 'Proceed to checkout') {
$translated = 'Ir a Pagar';
}
return $translated;
}
add_filter('gettext', 'custom_translate_text', 20, 3);
Then, there is one with JavaScript:
function custom_proceed_to_checkout_text() {
?>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
// Espera a que se cargue el DOM
var checkoutButton = document.querySelector('.wp-block-woocommerce-proceed-to-checkout-block a.wc-block-components-button');
if (checkoutButton) {
// Cambia el texto del botón
checkoutButton.textContent = 'Ir a Pagar';
}
});
</script>
<?php
}
add_action('wp_footer', 'custom_proceed_to_checkout_text');
I would greatly appreciate if someone could provide a solution or suggest a different approach to achieve this text change on the checkout button and the ‘Remove item’ link.
Thank you in advance!
2
Answers
I’m making the assumption that your selector is correct. Be sure to test that if this doesn’t work. My solution supposes that this button text in coming into being after the page loads and so you need to come along and set the text after. Also, note the comment to add some conditional logic so that the following only runs when it needs to rather than on every page of the site.
I needed something similar and I found a temporary fix for an old bug (now fixed) that has worked for me. The issue is archived here:
Translation of "Proceed to Checkout" button isn’t working #5452
The relevant javascript code of the fix is: