If you’re using WooCommerce and Polylang together to create a multilingual eCommerce store on WordPress, you might have encountered issues with translated WooCommerce pages, such as:
- Switching languages on product pages leading to a 404 error
- The “Proceed to Checkout” button not working
- Other broken links within WooCommerce pages (Cart, Checkout, etc.)
These issues occur because WooCommerce doesn’t natively support Polylang’s translation functionality out of the box. But don’t worry! Below, I’ll show you how to fix this using a simple piece of code that ensures WooCommerce can find the correct translated pages for each language.
The Fix: Ensuring WooCommerce Finds Translated Pages with Polylang
By applying the following code, we ensure WooCommerce works smoothly with Polylang. This code checks whether both WooCommerce and Polylang are activated and then ensures WooCommerce dynamically uses the correct translated version of each essential WooCommerce page (Cart, Checkout, My Account, Shop, etc.).
This solution is based on the integration I found on GitHub by 8manos. Below is a slightly modified version of the fix, which you can implement easily.
Step-by-Step Instructions
1. Add the Code to Your Theme
You need to add this code to your theme’s functions.php
file or create a custom plugin so that your fix doesn’t get overwritten by theme or plugin updates.
Here’s the code that integrates WooCommerce with Polylang:
// Only initialize the plugin if WooCommerce is active! if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { if (function_exists('pll_get_post')) { // is Polylang activated? // Add filters to ensure WooCommerce gets the correct translated pages add_filter('woocommerce_get_cart_page_id', 'pll_woocommerce_get_page_id'); add_filter('woocommerce_get_checkout_page_id', 'pll_woocommerce_get_page_id'); add_filter('woocommerce_get_edit_address_page_id', 'pll_woocommerce_get_page_id'); add_filter('woocommerce_get_myaccount_page_id', 'pll_woocommerce_get_page_id'); add_filter('woocommerce_get_pay_page_id', 'pll_woocommerce_get_page_id'); add_filter('woocommerce_get_shop_page_id', 'pll_woocommerce_get_page_id'); add_filter('woocommerce_get_terms_page_id', 'pll_woocommerce_get_page_id'); add_filter('woocommerce_get_view_order_page_id', 'pll_woocommerce_get_page_id'); // Function to return the translated page ID function pll_woocommerce_get_page_id($id) { return pll_get_post($id); // translate the page to the current language } } }
2. What This Code Does
- Checking if WooCommerce is active: The code first checks whether WooCommerce is active by seeing if
woocommerce/woocommerce.php
is among the active plugins. This ensures the fix only applies when WooCommerce is running. - Checking if Polylang is active: It also checks if the Polylang function
pll_get_post()
is available, meaning Polylang is activated and can translate the pages. - Filtering WooCommerce Pages: The core of the fix comes from adding filters that hook into WooCommerce’s system and ensure it retrieves the correct translated pages for key WooCommerce pages:
- Cart page
- Checkout page
- My Account page
- Shop page
- Pay page
- Terms page
- View Order page
- Edit Address page
- Translating the page ID: The
pll_get_post($id)
function is used to return the translated version of each WooCommerce page in the currently active language.
3. Why This Works
WooCommerce normally doesn’t know how to handle translated pages provided by Polylang. The fix ensures that whenever WooCommerce looks for important system pages (Cart, Checkout, My Account, etc.), it asks Polylang for the translated version of the page, based on the current language.
This solution ensures that when users switch languages, they will see the correct WooCommerce pages in the selected language, without 404 errors or broken links.
Views: 12