Skip to content Skip to sidebar

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to translate a string that is in a PHP snippet.
Solution:
If you're experiencing this issue, we recommend wrapping the text in a WordPress gettext function so that WPML can detect it when scanning the code. Here's a step-by-step guide:

add_action( 'woocommerce_check_cart_items', 'show_free_shipping_notice' );<br /><br />function show_free_shipping_notice() {<br />    $customer = WC()->customer;<br /><br />    if ( ! $customer ) {<br />        return;<br />    }<br /><br />    $country = $customer->get_shipping_country();<br /><br />    if ( empty( $country ) ) {<br />        $country = $customer->get_billing_country();<br />    }<br /><br />    if ( $country !== 'LV' ) {<br />        return;<br />    }<br /><br />    $total = WC()->cart->get_cart_contents_total() + WC()->cart->get_cart_contents_tax() + WC()->cart->get_fee_total();<br /><br />    if ( $total < 60 ) {<br />        $remaining = 60 - $total;<br /><br />        wc_add_notice(<br />            sprintf(<br />                /* translators: %.2f is the remaining amount needed for free shipping. */<br />                __( 'Bezmaksas piegāde Latvijā pasūtījumiem no 60€. Jums trūkst vēl %.2f€.', 'wpml-custom-snippets' ),<br />                $remaining<br />            ),<br />            'notice'<br />        );<br />    }<br />}<br />

After updating the snippet, navigate to WPML → Theme and plugins localization, scan the relevant source if available, then go to WPML → String Translation and search for the text to add its translation. For more details, visit our developer documentation: https://wpml.org/documentation/support/how-to-use-gettext/

Please note that this solution might be irrelevant if it's outdated or not applicable to your case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If this does not resolve your issue, please open a new support ticket at WPML support forum.

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.

Tagged: 

This topic contains 2 replies, has 1 voice.

Last updated by olafsO 1 week, 2 days ago.

Assisted by: Lucas Vidal de Andrade.

Author Posts
April 30, 2026 at 4:41 am #18004353

olafsO

How to translate the string that is in PHP snippet?

April 30, 2026 at 4:54 am #18004366

olafsO

Heya! It will circle back and forth. I need to translate a text, that is coming from WPCode snippet [it adds a Notice to shop cart/checkout].
Would be awesome to know how it is done to not bother you again next time.
I had a similar ticket, but the system does not save all ticket here unfortunately.

- Olafs

add_action( 'woocommerce_check_cart_items', 'show_free_shipping_notice' );
function show_free_shipping_notice() {
$customer = WC()->customer;
if ( ! $customer ) return;

$country = $customer->get_shipping_country();
if ( empty( $country ) ) {
$country = $customer->get_billing_country();
}

if ( $country !== 'LV' ) return;

$total = WC()->cart->get_cart_contents_total() + WC()->cart->get_cart_contents_tax() + WC()->cart->get_fee_total();
if ( $total < 60 ) {
$remaining = 60 - $total;
wc_add_notice(
sprintf(
'Bezmaksas piegāde Latvijā pasūtījumiem no 60€. Jums trūkst vēl %.2f€.',
$remaining
),
'notice'
);
}
}

May 1, 2026 at 8:13 am #18007402

Lucas Vidal de Andrade
WPML Supporter since 11/2023

Languages: English (English ) Spanish (Español ) German (Deutsch ) Portuguese (Brazil) (Português )

Timezone: Europe/Vienna (GMT+02:00)

Hi Olafs,

Thank you for contacting WPML Support.

Custom code is outside the scope of our support, but I’ll go the extra mile and share how this can usually be handled.

The text needs to be wrapped in a WordPress gettext function, so WPML can detect it when scanning the code. In your case, you can try this version:

add_action( 'woocommerce_check_cart_items', 'show_free_shipping_notice' );

function show_free_shipping_notice() {
	$customer = WC()->customer;

	if ( ! $customer ) {
		return;
	}

	$country = $customer->get_shipping_country();

	if ( empty( $country ) ) {
		$country = $customer->get_billing_country();
	}

	if ( $country !== 'LV' ) {
		return;
	}

	$total = WC()->cart->get_cart_contents_total() + WC()->cart->get_cart_contents_tax() + WC()->cart->get_fee_total();

	if ( $total < 60 ) {
		$remaining = 60 - $total;

		wc_add_notice(
			sprintf(
				/* translators: %.2f is the remaining amount needed for free shipping. */
				__( 'Bezmaksas piegāde Latvijā pasūtījumiem no 60€. Jums trūkst vēl %.2f€.', 'wpml-custom-snippets' ),
				$remaining
			),
			'notice'
		);
	}
}

Please note that this code has not been tested on your site. Before making any changes, make sure you have a full website backup.

After updating the snippet, please go to WPML → Theme and plugins localization, scan the relevant source if available, then go to WPML → String Translation and search for the text to add its translation.

You can find more details in our developer documentation here:
https://wpml.org/documentation/support/how-to-use-gettext/

Best regards,

May 7, 2026 at 8:14 am #18019419

olafsO

Thank you. I hardcoded the text to solve it but saving your solution for the future!

- Olafs