Translate Woocommerce Add to cart button & Cart widget - php

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.

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.

Related

How To Change The Button Text "Add to cart" in Wooccomerce With The Divi Theme

I am trying to change the Add To Cart text on the product page button to Buy Now
I've tried using the function that's being posted everywhere to solve the issue but somehow that doesn't seem to do the trick, also using a WordPress plugin is not changing the text.
So far I've found the following function
// 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 __( 'Buy 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 __( 'Buy Now', 'woocommerce' );
}
Adding that to my Code Inserter creates an error and breaks the website.
I think that Divi is overruling the function from somewhere. Any tips or ideas?

Change Add to Cart text to "Apply" on WooCommerce single product pages only

I am using the code below to replace add to cart button with a linked button to the product in Shop, related products and archives pages only
add_action( 'woocommerce_after_shop_loop_item', 'shop_view_product_button', 10);
function shop_view_product_button() {
global $product;
$link = $product->get_permalink();
echo 'View Product';
}
But I need to change the add to cart button text on the single product page to "Apply".
Any help?
Use the following:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_single_add_to_cart_custom_text', 20, 1 );
function product_single_add_to_cart_custom_text( $text ) {
$text = __( 'Apply', 'woocommerce' );
return $text;
}

WooCommerce custom PHP code to change Add To Cart button text

I want to update the default 'Add To Cart' text to be more suitable for my website. I have created a child theme using Astra, installed it, and then on the Theme Editor in WordPress I added a new function to my functions.php file that should have done the trick
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
function woocommerce_custom_product_add_to_cart_text() {
return __( 'Check it out', 'woocommerce' );
}
From all the examples online it should be this easy. I just read from another post that this may be deprecated now? Should this still work, anything else I can check? I want this to be the button text globally on all products but each product still shows 'Add To Cart'. I saved and then reloaded my website but do not see any changes
add_filter( 'woocommerce_product_add_to_cart_text', 'mujuonly_add_to_cart', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'mujuonly_add_to_cart', 10, 2 );
function mujuonly_add_to_cart( $text, $class ) {
return __( 'Check it out', 'woocommerce' );
}
Tested OK with WC 4.9

How can I replace the “Add to cart” button with an “Call to order” button and stop working on some product?

I am using woocommerce plugin in a wordpress site to sell some products. There are two types of products. some products will have add to cart button, but some will have call to order button instead of add to cart. But Both type of products will show the price and discounted price.
I am using some code to change the text
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text() {
return __( 'Call to order', 'woocommerce' );
}
that works. But I when I am using this code to change the url or say stop the button to add to cart that is not working.
add_filter( 'woocommerce_product_add_to_cart_url', 'woo_more_info_link' );
function woo_more_info_link( $link ) {
global $product; // switches link in all cases, i.e. in plugins
$link = get_permalink( $product->id );
return $link;
}
I actually want that the add to cart button should not working and should say only call to order.
Can anybody tell me how to stop add to cart from working.

How to change Add to cart text as my own text in Woocommerce

I am using Woocommerce V2.1.2 in Latest version of wordpress. I have googled, they have given a solution to change in frontend. But I need to change from backend.
Kindly suggest any idea's to fix the above issue.
This will change Add to cart button text in woocommerce...
I am not sure but this will help you in < 2.1 versions...I don't know about 2.1+
Add this code into your theme's functions.php file.
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' );
//For Single Product Page.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
//For Archives Product Page.
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text()
{
return __( 'My Button Text', 'woocommerce' );
}
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' );
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' );
add_filter('woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text');
function woo_custom_cart_button_text() {
return __( 'Buy Now', 'woocommerce' );
}

Categories