1

I have a problem with translating the Add to cart button on woocommerce.

My website is: http://test.mk/OPA The website is in albanian and english. First language is English.

I have installed Polylang Pro plugin and also tried with Loco translate.

I have changed the "name" of the Add to cart button with this code in functions.php:

 //To change add to cart text on single product page
     add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' ); 
function woocommerce_custom_single_add_to_cart_text() {
return __( 'Order Now', 'woocommerce' ); 
}

// To change add to cart text on product archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );  
function woocommerce_custom_product_add_to_cart_text() {
    return __( 'Order Now', 'woocommerce' );
}

So, what I really need to translate is Order Now.

I added the string using this code:

add_action('init', function() {
  pll_register_string('woocommerce_product_add_to_cart_text', 'Order Now', 'WooCommerce');
});

It is in strings in the polylang plugin, but it is not translating.

Does someone know how to help me to translate the button & the Cart wiget that is also in the website?

Thank you in advance.

1 Answers1

0

You can try with this function:

// change the text of the add to cart button according to the language
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text', 99, 1 ); 
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text', 99, 1 );
function woocommerce_custom_single_add_to_cart_text( $text ) {

    // returns the current language "Polylang"
    switch ( pll_current_language() ) {
        case 'en':
            return __( 'Order Now', 'woocommerce' );
        case 'sq':
            return __( 'Porosit tani', 'woocommerce' );
    }

    return $text;
}

Change the text of the "Add to cart" button according to the current language of the site.

This function should work but I believe you should search the Polylang plugin documentation to correctly translate the string.

The code must be inserted in the functions.php file of the active theme.

Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32