Update January 2017: This blog post is no longer up-to-date so the fix written may not work as expected anymore. I’m sorry 🙂
If you are building a multilingual ecommerce WordPress website using the plugins WooCommerce and Polylang, you might run into a bug. Thus you can translate your products and product categories in your wp-admin, WooCommerce can not figure out to shift properly between your translations on the frontend. This happens because WooCommerce does not natively support Polylang’s translations. Learn here how to fix this bug.
While developing a multilingual ecommerce website with WordPress using WooCommerce and Polylang, I ran into this strange bug on the frontend. I had all my products, taxonomies and pages in WooCommerce translated with Polylang, and still it didn’t work.
The issues occurred were:
- shifting language on a product resulted in 404 error page
- “Proceed to checkout” button on Cart page did not work
- various broken links in WooCommerce
I deep-dived into the codebase of WooCommerce and found that WooCommerce does not natively support or understand how Polylang’s translations work.
In my debugging research, I did find a way to fix it. Here goes:
How to fix WooCommerce to work with Polylang translations:
Step 1: Open the file wc-page-functions.php
Navigate to this file “wp-content/plugins/woocommerce/includes/wc-page-functions.php” and open it in a text editor.
Step 2: Search and replace code
Find the function called “wc_get_page_id( $page )” and then this line:
$page = apply_filters( 'woocommerce_get_' . $page . '_page_id', get_option('woocommerce_' . $page . '_page_id' ) );
and replace it with:
$page = function_exists('pll_get_post') ? apply_filters( 'woocommerce_get_' . $page . '_page_id', pll_get_post ( get_option('woocommerce_' . $page . '_page_id' ) ) ) : apply_filters( 'woocommerce_get_' . $page . '_page_id', get_option('woocommerce_' . $page . '_page_id' ) );
Step 3: Done!
Fixed! Now WooCommerce understands your Polylang-translated pages 🙂
Curious about what the code fix does? In short it does:
- Checking that Polylang is installed and the function “pll_get_post” exists.
- If above line is true, WooCommerce must look for the translated page of whatever page WooCommerce is looking for
- … else just use the standard page.
Code fix doesn’t work? Correct your settings in WooCommerce and Polylang
If applying the code fix does not solve your issue, please go through this guide and make sure your settings are exactly the same as described here.
These settings are proven to make WooCommerce and WordPress understand the translation-links between WooCommerce pages, cart, checkout etc.
In this example I have two languages enabled: English as default/base language and Danish as second language.
- Translate the WooCommerce required pages
Login to your WordPress admin and go to “Pages”. Make sure you have created the three minimum required pages: Cart, Checkout and Terms – and you have them “translated” to the other language(s) as well.
- Correct your Checkout settings in WooCommerce
Go to WooCommerce > Settings > Checkout.
Make sure your pages are selected in the dropdowns in “Checkout Pages”.
- Correct your Languages settings in Polylang
Go to Settings > Languages (Polylang) > Settings.
Make sure your settings match the settings on this screenshot:
- Done! You’re good to go!
The code fix in this blog post is not the best way to solve the issue. However it is currently the only way, as WooCommerce does not natively support Polylang’s translations on the frontend.
Replacing code in the plugin is a bad solution because our change can get removed/overridden when a new update of WooCommerce is installed.
Therefore: if you do use this fix, please be careful when updating WooCommerce in the future.
Which versions does it work with?
2015-06-26: Tested working with WooCommerce version 2.3.11 and Polylang version 1.7.6
2015-08-12: Tested working with WooCommerce version 2.4.3 and Polylang version 1.7.8
2015-09-01: Tested working with WooCommerce version 2.4.6 and Polylang version 1.7.9
2015-10-12: Tested working with WooCommerce version 2.4.7 and Polylang version 1.7.10
2015-12-01: Tested working with WooCommerce version 2.4.10 and Polylang version 1.7.12
I love your feedback!
I love to hear from you guys on how it works out for you. So please feel free to comment and share. I am happy to help as well 🙂