I want to change the translation value of a text when the language is the default English USA locale.
What is the correct way to accomplish this so we don't have to change the file every time we upgrade the wordpress version?
This is a nice plugin that does just that: http://wordpress.org/plugins/quick-localization/
But if you have only a few, you can also use this code:
function filter_gettext($translation, $text, $domain) {
if ( $text == 'Recent Comments' ) {
$translations = &get_translations_for_domain( $domain );
return $translations->translate( 'Something else' );
}
return $translation;
}
add_filter('gettext', 'filter_gettext', 10, 4);
Related
I’m trying to modify the word “reservado” or “backordered” in the order details page.
I use the following code, unfortunately without the desired result. The "backordered" text does not change, any advice?
function custom_backorder_message( $text, $product ){
if ($product->is_on_backorder( 0 ) ) {
$text = __( 'This item may take 3-4 weeks to deliver' );
}
return $text;
}
add_filter( 'woocommerce_get_availability_text', 'custom_backorder_message', 10, 2 );
If you want to change it via code you can use the woocommerce_backordered_item_meta_name filter hook.
So you get:
function filter_woocommerce_backordered_item_meta_name( $string, $item ) {
// Replace with new text
$string = 'My new text';
return $string;
}
add_filter( 'woocommerce_backordered_item_meta_name', 'filter_woocommerce_backordered_item_meta_name', 10, 2 );
But you could also change it in the language file.
Is there a hook to change the Total title from the WooCommerce order received page's order overview? Please see the below image to understand better
Yes, you can use the gettext WordPress filter to "translate" (rename in your case) that string of text.
Your string is inside the thankyou.php WooCommerce template:
esc_html_e( 'Total:', 'woocommerce' );
Based on WooCommerce: How to Translate / Rename Any String tutorial, the right code should be the following:
add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 9999, 3 );
function bbloomer_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( ! is_admin() && 'woocommerce' === $domain ) {
switch ( $translated ) {
case 'Total:':
$translated = 'Whatever:';
break;
}
}
return $translated;
}
I have problem with my woocommerce downloads section on WooCommerce "my account" > "Downloads": On product variations name, there is a <span> html tag that are visible:
I have tried to remove those "span" tags using:
add_filter( 'woocommerce_customer_available_downloads', 'remove_span_dl_name', 10, 7);
function remove_span_dl_name( $download ){
return str_replace( '<span> - </span>', ' - ',$download['download_name']);
}
but it removes all downloads altogether.
And I have also tried to remove those "span" tags using:
add_filter( 'woocommerce_available_download_link', 'remove_span_dl_name', 10, 7);
function remove_span_dl_name( $download ){
return str_replace( '<span> - </span>', ' - ',$download['download_name'] );
}
What is my mistake and how can I get rid of these tags?
The hook woocommerce_available_download_link is included in myaccount/my-downloads.php deprecated template since WooCommerce version 2.6 (the right template file that is used is myaccount/downloads.php).
Now as downloads are loaded through WC_Customer method get_downloadable_products(), you can to use woocommerce_customer_get_downloadable_products filter hook included in this method. Or also woocommerce_customer_available_downloads filter hook too.
To remove tags from product name, you can use str_replace() or much better and efficient strip_tags():
1). First way - Using strip_tags() function:
add_filter( 'woocommerce_customer_get_downloadable_products', 'remove_span_tags_from_product_name' );
// Or also
// add_filter( 'woocommerce_customer_available_downloads', 'remove_span_tags_from_product_name' );
function remove_span_tags_from_product_name( $downloads ){
// Only on my account downloads section
if ( ! is_wc_endpoint_url('downloads') )
return $downloads;
// Loop though downloads
foreach( $downloads as $key => $download ) {
// remove "span" html tags
$downloads[$key]['product_name'] = strip_tags( $download['product_name'] );
}
return $downloads;
}
2). Another way - Using str_replace() function:
add_filter( 'woocommerce_customer_get_downloadable_products', 'remove_span_tags_from_product_name' );
// Or also
// add_filter( 'woocommerce_customer_available_downloads', 'remove_span_tags_from_product_name' );
function remove_span_tags_from_product_name( $downloads ){
// Only on my account downloads section
if ( ! is_wc_endpoint_url('downloads') )
return $downloads;
// Loop though downloads
foreach( $downloads as $key => $download ) {
// remove "span" html tags
$downloads[$key]['product_name'] = str_replace( array('<span>', '</span>'), array('', ''), $download['product_name'] );
}
return $downloads;
}
Code goes in functions.php file of your active child theme (or active theme). Any way should work.
If you want that to work also everywhere ("order view", "Order received", emails notifications), remove:
// Only on my account downloads section
if ( ! is_wc_endpoint_url('downloads') )
return $downloads;
I'd like to override a function in woocommerce, specifically -
woocommerce/includes/wc-cart-functions.php (the wc_cart_totals_order_total_html function).
I could edit the function directly (it outputs html that needs to be changed), but I'd prefer not to as I'll lose changes on updates.
I'm sure it's a common thing, I'm just not quite sure how to go about doing that. If I copy the function to functions.php in my theme, I get an error about re-declaring the function.
It's an old topic, but maybe I could help a bit. I had similar problem. I wanted to override currencies, and add a custom currency. The functions are in woocommerce/includes/wc-core-functions.php
function get_woocommerce_currencies() {
return array_unique(
apply_filters( 'woocommerce_currencies',
array(
The other function is:
function get_woocommerce_currency_symbol( $currency = '' ) {
if ( ! $currency ) {
$currency = get_woocommerce_currency();
}
switch ( $currency ) {
...
return apply_filters( 'woocommerce_currency_symbol', $currency_symbol, $currency );
This is the code I've put in functions.php of my child theme:
add_filter( 'woocommerce_currencies', 'add_my_currency' );
function add_my_currency( $currencies ) {
$currencies['RSD'] = __( 'Serbian dinar', 'woocommerce' );
return $currencies;
}
I'm trying to change the default label for the Wordpress lost password form which says "Username or E-mail". I need it to just read "Email" and I don't want to edit any core Wordpress files. I've already achieved this for the default login form using:
global $pagenow;
if ($pagenow==='wp-login.php') {
add_filter( 'gettext', 'user_email_login_text', 20, 3 );
function user_email_login_text( $translated_text, $text, $domain ) {
if ($translated_text === 'Username') {
echo $translated_text;
$translated_text = 'Email';
}
return $translated_text;
}
}
And this seems to also work on the other default forms for labels consisting of single words. However, if I augment the if condition to include | $translated_text === 'Username or e-mail' it doesn't work.
Any ideas? I really don't want to have to code a bespoke form as this doesn't fit in with how my system works.
Also, I wonder why Wordpress spells "Email" as "E-mail" on the lost password form, but without the dash on all other forms...
I dont know if you allready solved this. Anyway your code is allmost correct. You were only missing that the string is case sensitive and to include the colon. Like this: if ($translated_text === 'Username or E-mail:')
No need to echo $translated_text either
This works on my installation:
global $pagenow;
if ($pagenow==='wp-login.php') {
add_filter( 'gettext', 'user_email_login_text', 20, 3 );
function user_email_login_text( $translated_text, $text, $domain ) {
if ($translated_text === 'Username or E-mail:') {
$translated_text = 'Email';
}
return $translated_text;
}
}