skip to Main Content

I need to change the default “Add To Cart” button text to ”Buy now“. I solved it by adding such a function to the function.php file of the child theme:

add_filter('woocommerce_product_add_to_cart_text', 'replace_cart_text');
add_filter('woocommerce_product_single_add_to_cart_text', 'replace_cart_text');
function replace_cart_text()
{
    return 'Buy now';
}

The problem is that I use different languages on the site. How to modify function to display different text on the buttons for each language? My translation plugin is Polylang.

2

Answers


  1. Chosen as BEST ANSWER

    I solved this problem by adding condition to the function:

    add_filter('woocommerce_product_add_to_cart_text', 'replace_cart_text');
    add_filter('woocommerce_product_single_add_to_cart_text', 'replace_cart_text');
    function replace_cart_text() {
        if (get_locale() == 'de_DE') {
            return 'Kaufen';
        }
        if (get_locale() == 'en_US') {
            return 'Buy now';
        }
    }
    

  2. You can try with the plugin references in the below link for a better translation for woocommerce.

    https://www.cloudways.com/blog/woocommerce-multilingual/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search