Wordpress - Get current language on functions.php - wordpress

I have the following code added on my theme's functions.php file, in order to translate some rebel strings I was unable to translate by the ordinary way / plugins:
function wc_billing_field_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Billing Address' :
$translated_text = __( 'Detalles de facturación', 'woocommerce' );
break;
case 'Shipping Address' :
$translated_text = __( 'Dirección de envío', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );
Now I have installed WPML and I need this code to run only when the languaje is Spanish.

ICL_LANGUAGE_CODE contain current language https://wpml.org/documentation/support/wpml-coding-api/
But, if you already using WPML — why you don't get rid of this code at all and translate all string inside WPML admin?

Related

Add product link to out of stock email notification in WooCommerce

I need to get the product link into out of stock emails sent to admin.
This code does not have the desired result even though I am, as far as I know, using the right filter hook? Any advice?
add_filter('woocommerce_email_content_no_stock', 'add_product_url_to_out_of_stock_email', 10, 2);
function add_product_url_to_out_of_stock_email( $message, $product ) {
global $product; // with or without this is the same result
return $message ."<br>\n". get_edit_post_link( $product->get_id() );
}
You can add/use WC_Product::get_permalink() – Product permalink to customize $message to suit your needs.
So you get:
function filter_woocommerce_email_content_no_stock ( $message, $product ) {
// Edit message
$message = sprintf( __( '%s is out of stock.', 'woocommerce' ), '' . html_entity_decode( wp_strip_all_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) . '' );
return $message;
}
add_filter( 'woocommerce_email_content_no_stock', 'filter_woocommerce_email_content_no_stock', 10, 2 );
IMPORTANT: This answer will not work by default because wp_mail() is used as mail function, where the content type is text/plain which does not allow using HTML
So to send HTML formatted emails with WordPress wp_mail(), add this extra piece of code
function filter_wp_mail_content_type() {
return "text/html";
}
add_filter( 'wp_mail_content_type', 'filter_wp_mail_content_type', 10, 0 );
Related: Adding product hyperlink to low stock notification email in WooCommerce

Anyway to change the Total title from the WooCommerce order received page's order overview

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;
}

Change wording on Woocommerce Checkout page

Thanks to help here:
Change Billing details text to Shipping details
I could change Billing details text on Woocommerce Checkout page by adding this to functions.php in Child theme:
function wc_billing_field_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Billing details' :
$translated_text = __( 'Billing Info', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );
What are the codes to change these 3 additional texts with different wording:
Additional information, PRODUCT & QUANTITY
(see screenshot):
Woocommerce Checkout Page
It would be better to override the checkout/form-checkout.php and checkout/review-order.php
Or You can use gettext filter .
This filter hook is applied to the translated text by the
internationalization functions (__(), _e(), etc.)
function th_wc_order_review_strings( $translated_text, $text, $domain ) {
if(is_checkout()){
switch ($translated_text) {
case 'Billing details' :
$translated_text = __( 'Billing Info', 'woocommerce' );
break;
case 'Additional information':
$translated_text = __('New Field Name', 'woocommerce');
break;
case 'Your order':
$translated_text = __('My Order', 'woocommerce');
break;
case 'Product':
$translated_text = __('Your Product', 'woocommerce');
break;
}
}
return $translated_text;
}
add_filter( 'gettext', 'th_wc_order_review_strings', 20, 3 );
Try changing the above code to your requirements.

How to translate a string in Woocommerce single language website

I need to translate __( 'privacy policy', 'woocommerce' ) which can be found in Woocomerce/includes/wp-template-functions.php. Full code for this is here
$privacy_link = $privacy_page_id ? '' . __( 'privacy policy', 'woocommerce' ) . '' : __( 'privacy policy', 'woocommerce' );
This is included inside textarea in Woocommerce->settings->Account and privacy, looks like a shortcode
Ukoliko ste pročitali i razumijeli naša [privacy_policy] molimo Vas da...
This is actually translated in Croatian language but I would need to change words slightly, how can I do it? Website is single language and I am not able to translate it with Gettext like I did some other strings
function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'privacy policy' :
$translated_text = __( 'pravila privatnosti', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 200, 3 );
Try Loco Translate plug-in (if you're using WordPress). It allows you to translate a lot of things down to the very detail. You can find the plug-in here.

Change title "Additional Information" in woocommerce

I want to change the title from the checkout page. But I just can change the label and the placeholder.
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Please type your PO number here and we will add it to the invoice.';
$fields['order']['order_comments']['label'] = '';
return $fields;
}
https://docs.woocommerce.com/document/editing-product-data-tabs/#
/**
* Rename product data tabs
*/
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
$tabs['reviews']['title'] = __( 'Ratings' ); // Rename the reviews tab
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
return $tabs;
}
As it stands there is not hook to change the section title. But here's the hack if you are desperate enough to make the modification.
Locate your template folder
Create a folder named 'checkout'
Locate the file form-shipping.php in the Woocommerce plugin foler under templates/checkout/
Copy file in step 3 to the folder created in step 2
Now you have superpowers over the checkout form
Edit this line:
<h3><?php _e( 'Additional Information', 'woocommerce' ); ?></h3>
This solution worked for me:
function ajg_relabel_additional_information_tab(){
return __( 'Specification', 'text-domain' );
}
add_filter('woocommerce_product_additional_information_heading', 'ajg_relabel_additional_information_tab');
function th_wc_order_review_strings( $translated_text, $text, $domain ) {
if(is_checkout()){
switch ($translated_text) {
case 'Billing details' :
$translated_text = __( 'Billing Info', 'woocommerce' );
break;
case 'Additional information':
$translated_text = __('New Field Name', 'woocommerce');
break;
case 'Your order':
$translated_text = __('My Order', 'woocommerce');
break;
case 'Product':
$translated_text = __('Your Product', 'woocommerce');
break;
}
}
return $translated_text;
}
add_filter( 'gettext', 'th_wc_order_review_strings', 20, 3 );
The documentation of Woocommerce is not complety...
https://docs.woocommerce.com/document/editing-product-data-tabs/
You would check condition about the callback before add or replace some value in array, otherwise the tab will display with nothing inside.
/**
* Filter product data tabs
*/
function filter_product_tabs( $tabs ) {
global $product;
if ( isset($tabs['description']['callback']) ) {
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
$tabs['description']['priority'] = 5; // Description
}
if ( isset($tabs['additional_information']['callback']) ) {
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
$tabs['additional_information']['priority'] = 10; // Additional information
}
if ( isset($tabs['reviews']['callback']) ) {
$tabs['reviews']['title'] = __( 'Review' ) . ' (' . $product->get_review_count() . ') '; // Rename the reviews tab
$tabs['reviews']['priority'] = 15; // Reviews
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_product_tabs', 98 );
Why ? because the developper Woocommerce will check the content of array in the tab template
(version 3.8.0) (WC
/**
* Filter tabs and allow third parties to add their own.
*
* Each tab is an array containing title, callback and priority.
*
* #see woocommerce_default_product_tabs()
*/
$product_tabs = apply_filters( 'woocommerce_product_tabs', array() );
if ( ! empty( $product_tabs ) ) :
....
This worked for me if anyone is still after this change
//Shipping Weight custom tab name
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['additional_information']['title'] = __( 'Additional Information' ); // Rename the Additional Information text
return $tabs;
}

Resources