Skip Navigation

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

Problem:
The client added custom code to the functions.php file in their child theme to display stock status in a WooCommerce product dropdown but was unable to translate the 'In Stock' and 'Out of Stock' text.

Solution:
1. We advised the client to wrap the hardcoded strings with the gettext function

__()

to make them translatable. Specifically, replace:

return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';

with:

return $stock == 1 ? ' - ('.__('In Stock','themedomain').')' : ' - ('.__('Out of Stock','themedomain').')';

2. We instructed the client to scan the theme for strings from the WPML > Theme and plugins localization page.
3. We directed the client to go to WPML > String Translation, search for the 'In Stock' and 'Out of Stock' strings, and add the translations.

For more details, we provided documentation links:

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 1 reply, has 2 voices.

Last updated by Noman 1 year ago.

Assisted by: Noman.

Author Posts
February 4, 2024 at 7:18 am #15263027

ofirG

{code}
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock = $variation['is_in_stock'];
break;
}
}
return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}

// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :

$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );

if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}

$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );

foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $option );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';

endif;

return $html;
}
{/code}Hey, I added a code to functions.php on my child theme and I can't manage to translate it.
I attached a screenshot. I want to translate the "Out of stock" and "In stock".
I tried to sync on "Theme and plugins localization" but it didn't work out.
Also, I tried on "Translate texts in admin screens", and I found 2 matched strings of "Out of stock" and "In stock", I translated them but nothing has chagned.
Also tried to remove cache both from my website and the WPML plugin and still nothing chagned.
I attached also the code I added to my functions.php so you could see.

download.png
February 4, 2024 at 2:10 pm #15263591

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting WPML Support. You need to wrap the hardcoded strings in the gettext(__()) function to make them translatable.

1. Please replace the below code:

return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';

With:

return $stock == 1 ? ' - ('.__('In Stock','themedomain').')' : ' - ('.__('Out of Stock','themedomain').')';

2. Scan the theme in which you update the above code from WPML >> Theme and plugins localization page.

3. Go to WPML >> String Translation, search for these strings, and add the translation.

Here are docs for more details:
https://developer.wordpress.org/reference/hooks/gettext/
https://wpml.org/documentation/support/translating-the-theme-you-created/

Please let me know if this resolves your issue or if you need further assistance.

Thank you

February 5, 2024 at 5:53 am #15264229

ofirG

Thanks a lot, it solved it!