Last updated on October 18, 2023

WooCommerce multilingual setup

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

Implement WooCommerce multilingual setup with ease.

To set up WooCommerce multilingual on your WordPress website, you can use the WPML (WordPress Multilingual) plugin. WPML allows you to translate your WooCommerce products, categories, and other elements into multiple languages. Here’s an example code snippet that demonstrates how to set up WooCommerce multilingual using WPML:

/**
 * Enable WooCommerce multilingual support with WPML.
 */
function wpsnippets_enable_woocommerce_multilingual() {
    if ( function_exists( 'wcml_is_multi_currency_on' ) ) {
        global $woocommerce_wpml;
        remove_action( 'woocommerce_after_shop_loop_item', array( $woocommerce_wpml->multi_currency, 'show_price_in_second_currency' ), 10 );
    }
}
add_action( 'init', 'wpsnippets_enable_woocommerce_multilingual' );

This code snippet enables WooCommerce multilingual support by removing the action that displays the price in the second currency. It checks if the WPML multi-currency feature is enabled before removing the action.

To use this code snippet, you can add it to your theme’s functions.php file or create a custom plugin. After adding the code, make sure you have the WPML plugin installed and activated on your WordPress website.

This code snippet is useful when you want to set up WooCommerce multilingual using WPML and customize the behavior of the multi-currency feature.

Examples

Example 1: Setting up WooCommerce multilingual with WPML

This example demonstrates how to set up WooCommerce multilingual using the WPML plugin. The code snippet shows how to enable the translation of WooCommerce products, categories, and attributes.

// Enable translation for WooCommerce products
add_filter( 'wpml_translate_single_string', 'wpsnippets_translate_woocommerce_product', 10, 6 );
function wpsnippets_translate_woocommerce_product( $translated, $original, $domain, $language_code, $context, $plural ) {
    if ( 'woocommerce' === $domain && 'Product type' === $context ) {
        $translated = __( 'Product type', 'woocommerce' );
    }
    return $translated;
}

// Enable translation for WooCommerce product categories
add_filter( 'wpml_taxonomy_translation', 'wpsnippets_translate_woocommerce_category', 10, 3 );
function wpsnippets_translate_woocommerce_category( $taxonomy_translation, $taxonomy, $term_id ) {
    if ( 'product_cat' === $taxonomy ) {
        $taxonomy_translation['display_type'] = 'translated';
    }
    return $taxonomy_translation;
}

// Enable translation for WooCommerce product attributes
add_filter( 'wpml_custom_field_translation', 'wpsnippets_translate_woocommerce_attribute', 10, 4 );
function wpsnippets_translate_woocommerce_attribute( $field_translation, $field, $field_value, $field_type ) {
    if ( 'pa_' === substr( $field, 0, 3 ) ) {
        $field_translation['display_type'] = 'translated';
    }
    return $field_translation;
}

This code example adds filters to enable the translation of WooCommerce products, categories, and attributes using the WPML plugin. The wpsnippets_translate_woocommerce_product function translates the “Product type” string, wpsnippets_translate_woocommerce_category function sets the display type of product categories to “translated”, and wpsnippets_translate_woocommerce_attribute function sets the display type of product attributes to “translated”.

Example 2: Setting up WooCommerce multilingual with Polylang

This example demonstrates how to set up WooCommerce multilingual using the Polylang plugin. The code snippet shows how to enable the translation of WooCommerce products, categories, and attributes.

// Enable translation for WooCommerce products
add_filter( 'pll_translate_string', 'wpsnippets_translate_woocommerce_product', 10, 2 );
function wpsnippets_translate_woocommerce_product( $translation, $string ) {
    if ( 'Product type' === $string ) {
        $translation = __( 'Product type', 'woocommerce' );
    }
    return $translation;
}

// Enable translation for WooCommerce product categories
add_filter( 'pll_copy_taxonomies', 'wpsnippets_translate_woocommerce_category' );
function wpsnippets_translate_woocommerce_category( $taxonomies ) {
    $taxonomies[] = 'product_cat';
    return $taxonomies;
}

// Enable translation for WooCommerce product attributes
add_filter( 'pll_copy_post_metas', 'wpsnippets_translate_woocommerce_attribute' );
function wpsnippets_translate_woocommerce_attribute( $post_metas ) {
    $post_metas[] = 'pa_*';
    return $post_metas;
}

This code example adds filters to enable the translation of WooCommerce products, categories, and attributes using the Polylang plugin. The wpsnippets_translate_woocommerce_product function translates the “Product type” string, wpsnippets_translate_woocommerce_category function includes the product_cat taxonomy for translation, and wpsnippets_translate_woocommerce_attribute function includes all post meta fields starting with pa_ for translation.

Example 3: Setting up WooCommerce multilingual with Weglot

This example demonstrates how to set up WooCommerce multilingual using the Weglot plugin. The code snippet shows how to enable the translation of WooCommerce products, categories, and attributes.

// Enable translation for WooCommerce products
add_filter( 'weglot_get_translated_string', 'wpsnippets_translate_woocommerce_product', 10, 3 );
function wpsnippets_translate_woocommerce_product( $translated_string, $original_string, $language_code ) {
    if ( 'Product type' === $original_string ) {
        $translated_string = __( 'Product type', 'woocommerce' );
    }
    return $translated_string;
}

// Enable translation for WooCommerce product categories
add_filter( 'weglot_copy_taxonomy', 'wpsnippets_translate_woocommerce_category', 10, 2 );
function wpsnippets_translate_woocommerce_category( $copy_taxonomy, $taxonomy ) {
    if ( 'product_cat' === $taxonomy ) {
        $copy_taxonomy = true;
    }
    return $copy_taxonomy;
}

// Enable translation for WooCommerce product attributes
add_filter( 'weglot_copy_post_meta', 'wpsnippets_translate_woocommerce_attribute', 10, 3 );
function wpsnippets_translate_woocommerce_attribute( $copy_post_meta, $meta_key, $post_id ) {
    if ( 'pa_' === substr( $meta_key, 0, 3 ) ) {
        $copy_post_meta = true;
    }
    return $copy_post_meta;
}

This code example adds filters to enable the translation of WooCommerce products, categories, and attributes using the Weglot plugin. The wpsnippets_translate_woocommerce_product function translates the “Product type” string, wpsnippets_translate_woocommerce_category function enables the copy of the product_cat taxonomy for translation, and wpsnippets_translate_woocommerce_attribute function enables the copy of product attributes starting with pa_ for translation.

Last updated on October 18, 2023. Originally posted on January 21, 2024.

Leave a Reply