Last updated on October 18, 2023

WPML translate WooCommerce products

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Translate WooCommerce products with WPML.

To translate WooCommerce products using WPML, you can use the wpml_register_single_string function to register the strings that need translation. Then, you can use the wpml_translate_single_string function to retrieve the translated strings.

Here’s an example code snippet that demonstrates how to translate a WooCommerce product title using WPML:

/**
 * Register WooCommerce product title for translation.
 *
 * @param int $product_id The ID of the WooCommerce product.
 */
function wpsnippets_register_product_title_for_translation( $product_id ) {
    $product = wc_get_product( $product_id );
    $title = $product->get_name();

    // Register the product title for translation.
    wpml_register_single_string( 'WooCommerce', 'Product Title: ' . $product_id, $title );
}
add_action( 'woocommerce_product_save', 'wpsnippets_register_product_title_for_translation' );

/**
 * Translate WooCommerce product title.
 *
 * @param string $title The original product title.
 * @return string The translated product title.
 */
function wpsnippets_translate_product_title( $title ) {
    global $post;

    // Retrieve the product ID.
    $product_id = $post->ID;

    // Translate the product title.
    $translated_title = wpml_translate_single_string( 'WooCommerce', 'Product Title: ' . $product_id, $title );

    return $translated_title;
}
add_filter( 'the_title', 'wpsnippets_translate_product_title' );

In this example, the wpsnippets_register_product_title_for_translation function is hooked to the woocommerce_product_save action. It retrieves the product title using the get_name method and registers it for translation using the wpml_register_single_string function.

The wpsnippets_translate_product_title function is hooked to the the_title filter. It retrieves the original product title and translates it using the wpml_translate_single_string function. The translated title is then returned.

By using these code snippets, the WooCommerce product title will be registered for translation and the translated title will be displayed when the site is viewed in different languages using WPML.

Examples

Example 1: Translating WooCommerce Products using WPML

This use case demonstrates how to translate WooCommerce products using the WPML plugin. The code example shows how to programmatically translate a product’s title, description, and short description in different languages.

// Translate product title
$product_id = 123;
$product_title = get_the_title($product_id);
$translated_title = apply_filters('wpml_translate_single_string', $product_title, 'woocommerce', 'product_title', $language_code);
echo $translated_title;

// Translate product description
$product_description = get_post_field('post_content', $product_id);
$translated_description = apply_filters('wpml_translate_single_string', $product_description, 'woocommerce', 'product_description', $language_code);
echo $translated_description;

// Translate product short description
$product_short_description = get_post_meta($product_id, '_short_description', true);
$translated_short_description = apply_filters('wpml_translate_single_string', $product_short_description, 'woocommerce', 'product_short_description', $language_code);
echo $translated_short_description;

In this code example, we first retrieve the product’s title, description, and short description using appropriate WordPress functions. Then, we use the wpml_translate_single_string filter provided by WPML to translate each string based on the specified language code. Finally, we echo the translated strings.

Example 2: Translating WooCommerce Product Categories using WPML

This use case demonstrates how to translate WooCommerce product categories using WPML. The code example shows how to retrieve the translated category name for a specific language.

// Translate product category name
$category_id = 456;
$category_name = get_cat_name($category_id);
$translated_category_name = apply_filters('wpml_translate_single_string', $category_name, 'woocommerce', 'product_category_name', $language_code);
echo $translated_category_name;

In this code example, we retrieve the category name for a specific category ID using the get_cat_name function. Then, we use the wpml_translate_single_string filter to translate the category name based on the provided language code. Finally, we echo the translated category name.

Example 3: Translating WooCommerce Product Attributes using WPML

This use case demonstrates how to translate WooCommerce product attributes using WPML. The code example shows how to retrieve the translated attribute value for a specific language.

// Translate product attribute value
$attribute_name = 'color';
$attribute_value = 'red';
$translated_attribute_value = apply_filters('wpml_translate_single_string', $attribute_value, 'woocommerce', 'product_attribute_' . $attribute_name, $language_code);
echo $translated_attribute_value;

In this code example, we specify the attribute name and value that we want to translate. We then use the wpml_translate_single_string filter to translate the attribute value based on the provided language code. Finally, we echo the translated attribute value.

Last updated on October 18, 2023. Originally posted on December 22, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *