Solution for WooCommerce URLs and Polylang

Here I would like to explain to you how I have harmonized WooCommerce and Polylang URLs.

The Problem

You always bump into problems if you try to build two basic functions of a system on plug-ins.

In this case I worked with the plug-ins Polylang and WooCommerce. I have noticed that these plugins do not work well together, but this is normal and can happen when you are working with multiple plugins.

The visitors of the website, who are looking at the site in English, have filled their shopping cart and would like to look at it. You click on Shopping cart and will be redirected to a German-languaged cart.

They will change the language on the cart-page, but will have the same issue when they want to go to the checkout page.

The analysis of the problem
Then I looked at the template and had to realize that only the following is executed:

 echo esc_url( wc_get_checkout_url() ); 

So I looked at Google, whether someone was also confronted with this problem and found someone.

Jesper Lunds approach

The programmer Jesper Lund had already published a solution.
Let me explain you his solution approach:

WooCommerce offers the users in the backend the possibility to choose a page for cart, checkout and terms & conditions. This is unavoidable because the IDs of the pages can be different for everyone. For example if someone has already been created this pages and installed WooCommerce afterwards, he can select the already created pages there.


However, since the pages you translate have different IDs, they are not automatically recognized by WooCommerce, and the visitors to the website, no matter what language they use, will always be forwarded to the page you indicated there.

For this, WooCommerce will pick up the permalinks of the pages that were selected in the following file with the following function:

* Retrieve page permalink.
* @param string $page
* @return string
function wc_get_page_permalink( $page ) { $page_id = wc_get_page_id( $page ); $permalink = 0 < $page_id ? get_permalink( $page_id ) : get_home_url(); return apply_filters( 'woocommerce_get_' . $page . '_page_permalink', $permalink ); }
File: /woocommerce/includes/wc-page-functions.php

Jesper simply manipulated the variable $ page_id here by switching Polylang between

 $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' ) ); 
Jespers approach

This solution is very good, but there is a fundamental problem behind it!

With every update from WooCommerce, this file is updated and you have to replace it, which means that you can not program it once and let it run automatically.

The solution mentioned above is, of course, not long-lasting, so I looked around for other solutions. I found it and learned that one can use so-called filter hooks to manipulate WooCommerce values.

WooCommerce has three filters for this topic:

  • woocommerce_get_checkout_url
  • woocommerce_get_cart_url
  • woocommerce_get_terms_url

These three filters can be entered directly into the functions.php file as a long-lasting solution.

add_filter('woocommerce_get_checkout_url', function($url){
	return get_permalink(pll_get_post(get_option( 'woocommerce_checkout_page_id' )));
add_filter('woocommerce_get_cart_url', function($url){
	return get_permalink(pll_get_post(get_option( 'woocommerce_cart_page_id' )));
add_filter('woocommerce_get_terms_url', function($url){
	return get_permalink(pll_get_post(get_option( 'woocommerce_terms_page_id' )));
My solution