Last updated on October 18, 2023

WPML multilingual WooCommerce setup

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

Set up WooCommerce for multilingual sites with WPML.

To set up a multilingual WooCommerce website using WPML (WordPress Multilingual Plugin), you need to follow these steps:

  1. Install and activate the WPML plugin from the WordPress plugin repository.
  2. Configure the WPML plugin by going to WPML -> Languages in your WordPress admin dashboard.
  3. Select the languages you want to enable for your website and configure the language switcher options.
  4. Translate your WooCommerce products, categories, and other elements using the WPML Translation Editor or by manually creating translations for each language.
  5. Display the language switcher on your WooCommerce pages to allow users to switch between languages.

To display the language switcher, you can use the icl_get_languages() function provided by WPML. Here’s an example code snippet that retrieves the available languages and displays them as a dropdown:

function wpsnippets_display_language_switcher() {
    $languages = icl_get_languages('skip_missing=0&orderby=code');

    if (!empty($languages)) {
        echo '<ul class="language-switcher">';
        foreach ($languages as $language) {
            echo '<li>';
            echo '<a href="' . esc_url($language['url']) . '">' . esc_html($language['native_name']) . '</a>';
            echo '</li>';
        }
        echo '</ul>';
    }
}

You can place this code snippet in your theme’s functions.php file or in a custom plugin. Then, you can call the wpsnippets_display_language_switcher() function wherever you want to display the language switcher, such as in your header.php file.

Remember to style the language switcher according to your website’s design using CSS.

This code snippet retrieves the available languages using the icl_get_languages() function and then loops through each language to display it as a list item in an unordered list. The language’s native name is used as the link text, and the language’s URL is used as the link destination.

Examples

Example 1: Creating a Multilingual WooCommerce Store with WPML

This example demonstrates how to set up a multilingual WooCommerce store using the WPML plugin. The code example shows how to enable WPML’s WooCommerce Multilingual module and configure the language settings.

// Enable WooCommerce Multilingual module
add_action( 'wpml_loaded', 'wpsnippets_enable_woocommerce_multilingual' );
function wpsnippets_enable_woocommerce_multilingual() {
    global $woocommerce_wpml;
    $woocommerce_wpml->load_plugin_textdomain();
    $woocommerce_wpml->load_settings();
}

// Configure language settings
add_filter( 'wpml_setting', 'wpsnippets_configure_language_settings', 10, 2 );
function wpsnippets_configure_language_settings( $value, $option_name ) {
    if ( $option_name === 'icl_sitepress_settings' ) {
        $value['store_language'] = 'en'; // Set your default language code here
        $value['default_language'] = 'en'; // Set your default language code here
        $value['active_languages'] = array( 'en', 'fr' ); // Set your active language codes here
    }
    return $value;
}

In this code example, we use two functions to set up a multilingual WooCommerce store. The wpsnippets_enable_woocommerce_multilingual function enables the WooCommerce Multilingual module and loads its settings. The wpsnippets_configure_language_settings function configures the language settings, including the default language and active languages. Adjust the language codes according to your needs.

Example 2: Translating WooCommerce Product Categories

This example demonstrates how to translate WooCommerce product categories using WPML. The code example shows how to register custom taxonomy translations for product categories.

// Register custom taxonomy translations
add_filter( 'wpml_translation_element_type', 'wpsnippets_register_taxonomy_translations', 10, 2 );
function wpsnippets_register_taxonomy_translations( $element_type, $taxonomy ) {
    if ( $taxonomy === 'product_cat' ) {
        $element_type = 'tax_product_cat';
    }
    return $element_type;
}

In this code example, we use the wpml_translation_element_type filter to register custom taxonomy translations for WooCommerce product categories. The wpsnippets_register_taxonomy_translations function checks if the taxonomy is ‘productcat’ and sets the element type to ‘taxproduct_cat’. This allows WPML to handle the translation of product categories.

Example 3: Translating WooCommerce Product Attributes

This example demonstrates how to translate WooCommerce product attributes using WPML. The code example shows how to register custom taxonomy translations for product attributes.

// Register custom taxonomy translations
add_filter( 'wpml_translation_element_type', 'wpsnippets_register_taxonomy_translations', 10, 2 );
function wpsnippets_register_taxonomy_translations( $element_type, $taxonomy ) {
    if ( $taxonomy === 'pa_color' ) {
        $element_type = 'tax_pa_color';
    }
    return $element_type;
}

In this code example, we use the wpml_translation_element_type filter to register custom taxonomy translations for WooCommerce product attributes. The wpsnippets_register_taxonomy_translations function checks if the taxonomy is ‘pacolor’ (replace with your attribute taxonomy slug) and sets the element type to ‘taxpa_color’ (replace with your attribute taxonomy translation slug). This allows WPML to handle the translation of product attributes.

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

Leave a Reply

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