This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by Marcos Vinicios Barreto 4 months, 3 weeks ago.

Assigned support staff: Marcos Vinicios Barreto.

Author Posts
May 28, 2019 at 8:29 pm #3909997

Jerzy Afanasjew

Hello,

I have trouble translating a string from custom code in my functions.php. I was able to translate my custom line of code, but couldn't get it to still work with the original snippet.

this is the full function:

add_action( 'woocommerce_review_order_before_submit', 'bbloomer_checkout_add_on', 9999 );

function bbloomer_checkout_add_on() {
$product_ids = array( 14877, 14879, 15493 );
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( in_array( $product_in_cart, $product_ids ) ) {
$in_cart = true;
break;
}
}
if ( ! $in_cart ) {
echo '<h4><b>● Would you like to add something?</b></h4>';
}
}

I tried replacing "echo" line with:

function change_sm_location_search_title( $original_value ) {
return __("<h4><b>● Would you like to add something?</b></h4>","text-domain");
}
add_filter( 'sm-location-search-title', 'change_sm_location_search_title' );

It worked 50/50. While I could translate the string in WPML, it doesn't show up on front end at all if I keep it with the <h4> and <b> tags. How to translate it, so it will still show up AND not break the original function?

Thanks in advance!

May 29, 2019 at 12:24 am #3910669

Marcos Vinicios Barreto
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Bahia (GMT-03:00)

Hello,

Thank you for contacting WPML Support. I recommend you to keep your html tags out of your gettext function. You could try something like:

Instead of:

return __("<h4><b>Would you like to add something?</b></h4>","text-domain");

You can try:

return '<h4><b>' . __('Would you like to add something?','text-domain') . '</b></h4>';

You can learn more how to properly use the gettext functions at:
https://wpml.org/documentation/getting-started-guide/theme-localization/
hidden link

Please, let me know if it helps. Have a nice day.

May 29, 2019 at 5:46 am #3911949

Jerzy Afanasjew

Thank you for your reply! Unfortunately still the localized string doesn't show up on the front end.

So far only the " echo '<h4><b>● Would you like to add something?</b></h4>';" displays in the front end.

This is the code right now:

add_action( 'woocommerce_review_order_before_submit', 'bbloomer_checkout_add_on', 9999 );

function bbloomer_checkout_add_on() {
$product_ids = array( 14877, 14879, 15493 );
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( in_array( $product_in_cart, $product_ids ) ) {
$in_cart = true;
break;
}
}
if ( ! $in_cart ) {

function change_sm_location_search_title( $original_value ) {
return '<h4><b>' . __('● Would you like to add something?','text-domain') . '</b></h4>';
}
add_filter( 'sm-location-search-title', 'change_sm_location_search_title' );

echo '<p><a class="button" style="width: 140px" href="?add-to-cart=1183"> €1.2 (10) </a><a class="button" style="width: 140px" href="?add-to-cart=9945"> €2.1 (20)</a><a class="button" style="width: 140px" href="?add-to-cart=9948"> €3 (30)</a></p>';
}
}

Thanks in advance for your time!

May 29, 2019 at 5:15 pm #3917683

Marcos Vinicios Barreto
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Bahia (GMT-03:00)

Hello,

Thank you for the updates. I would like to request temporary access (wp-admin and FTP) to your site to take better look at the issue. You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.

Our Debugging Procedures

I will be checking various settings in the backend to see if the issue can be resolved. Although I won't be making changes that affect the live site, it is still good practice to backup the site before providing us access. In the event that we do need to debug the site further, I will duplicate the site and work in a separate, local development environment to avoid affecting the live site.

Privacy and Security Policy

We have strict policies regarding privacy and access to your information. Please see:
https://wpml.org/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

**IMPORTANT**

- Please make a backup of site files and database before providing us access.
- If you do not see the wp-admin/FTP fields this means your post & website login details will be made PUBLIC. DO NOT post your website details unless you see the required wp-admin/FTP fields. If you do not, please ask me to enable the private box. The private box looks like this: hidden link

Please, let me know if you need any additional details. Have a nice day.

June 4, 2019 at 1:25 am #3946785

Marcos Vinicios Barreto
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Bahia (GMT-03:00)

Hello,

Thank you for contacting WPML Support. I took a closer look in your website and noticed WPML is properly reading your custom function localized string as you can see in the attached screenshot. The reason why it doesn't show in your website frontend is related with your function logic instead of an issue which WPML, here we have a confirmation it is being properly read as mentioned before.

I recommend you to double check your function logic, specifically the use of nested functions in PHP which leads to confusion and logic error in some situations. Unfortunately in this particular case we are not able to help as it is related with code customizations, for that, please refer to some of our WPML contractors https://wpml.org/contractors/ to get more directions regarding this customization. Thank you for your understanding, have a nice day.