Weglot get_current_language redirection WordPress

Viewed 26

I made a redirection to a custom page when the cart is empty. Website is now multilingual, using weglot. When the language is french, it should redirect the user to the french version of the custom page, same scenario when language is italian.

The redirection per se works, but I always end up on the german (initial language) version.

Example: When the site language is french and you click on cart (which is empty), the browser makes the following redirections:

Start: https://example.com/fr --> Clicking on cart --> https://example.com/fr/cart --> https://example.com/fr/cart-empty (where it should stay) --> https://example.com/fr/?wg-choose-original=false --> https://example.com/cart-empty

// Redirect if cart is empty
add_action("template_redirect", 'redirection_function');
function redirection_function(){
    global $woocommerce;
    if(is_cart()){
        if( WC()->cart->cart_contents_count == 0 && weglot_get_current_language()=='fr'){
            wp_safe_redirect('https://example.com/fr/cart-empty' );
            exit;
        }

        if(WC()->cart->cart_contents_count == 0 && weglot_get_current_language()=='it'){
            wp_safe_redirect('https://example.com/it/cart-empty' );
            exit;
        }

        if(WC()->cart->cart_contents_count == 0){
            wp_safe_redirect('https://example.com/cart-empty' );
            exit;
        }
    }
}

What am I missing?

0 Answers
Related