• Resolved soylug

    (@soylug)


    I wonder if there is an easy way to translate links generated by other plugins. For example, I’m working with woocommerce and when you add an item to your cart, it shows a “View cart” option. Everything is in the language selected, but the “VIew cart” button always link to the same page in the same language, although I have translated it.
    I was reading I tried with stuff like
    add_filter(‘pll_the_language_link’, ‘post_type_link’);
    but didn’t work.
    If something like that could work, it will be very useful for other plugins.
    Thanks a lot, your plugin is great!

Viewing 10 replies - 16 through 25 (of 25 total)
  • Thread Starter soylug

    (@soylug)

    Thanks, but nothing changed… It works but without pll_get_post, so I always get the same language.
    Now I’m trying this:

    add_filter ('woocommerce_get_checkout_url', 'redirect_checkout_page_translated');
    function redirect_checkout_page_translated() {
    	if ( pll_current_language() == 'es'){
    		$url = 'http://www.xxxx'; //spanish link
    		}
    	else if ( pll_current_language() == 'en'){
    		$url = 'http://www.yyyy'; //english link
    		}
    	else{
    		$url = 'http://www.zzzz'; //other page
    	}
    	return $url;
      }

    I’ve tried all options (name, slug, locale) but doesn’t work. The las else is a default one because it seemed like pll_current_languge didn’t match options. Whith the code above, it always returns the default option (‘http://www.zzzz’). I have two languages, english and spanish, so I can’t understand what is pll_current_language function returning…
    I’m feeling silly and also becoming crazy πŸ™

    Plugin Author Chouby

    (@chouby)

    While debugging, you can try to display some messages, to check that your function is called, what a variable contains…
    For example, you can display the value return by pll_current_language with:

    print_r(pll_current_language());

    Another thing which may be useful is to set WP_DEBUG to true in your wp-config.php. This may display some errors which may help you.

    Thread Starter soylug

    (@soylug)

    I’m still trying to solve this… I’m usin print_r as suggested and I get something I think is strange. For this piece of code:

    add_filter('woocommerce_get_checkout_url', 'pll_woocommerce_get_checkout_url');
      function pll_woocommerce_get_checkout_url(){
    	$checkout_page_id= woocommerce_get_page_id('checkout');
    	print_r("  i'm here A with id: ");
    	print_r($checkout_page_id);
    	$checkout_page_id_lan = pll_get_post ($checkout_page_id);
    	print_r("  i'm here B with id: ");
    	print_r($checkout_page_id_lan);
    	print_r ("  i'm at c");
    }

    I get “i’m here A with id: 564 i’m here B with id: i’m at c” as answer, so Polylang is not translating the id… should it work?

    Plugin Author Chouby

    (@chouby)

    Maybe the language is not defined yet when woocommerce is using that function. Do you set the language from content in Polylang settings? It may be safer to set it from the language code as in that case Polylang sets the language sooner in the process.

    Thread Starter soylug

    (@soylug)

    Nice!
    I changed what you said and now i get “i’m here A with id: 564 i’m here B with id: 678 i’m at c” as answer, so I can continue working.
    Thanks a lot!!!

    Thread Starter soylug

    (@soylug)

    Thanks a lot, but finally I couldn’t get it working, so I gave up woocommerce. I set as resolved as you did solve my first question πŸ˜‰

    Thread Starter soylug

    (@soylug)

    solved first question

    Hi !

    Just a word to say thank you to Chouby.
    It has been really helpful!

    Thx !

    About checkout, it works for me like that:

    if (function_exists('pll_get_post')) { // is Polylang activated?
      add_filter('woocommerce_get_cart_page_id', 'pll_woocommerce_get_cart_page_id');
      add_filter('woocommerce_get_checkout_page_id', 'pll_woocommerce_get_checkout_page_id');
      function pll_woocommerce_get_cart_page_id($id) {
        return pll_get_post($id); // translate the page to current language
      }
      function pll_woocommerce_get_checkout_page_id($id) {
        return pll_get_post($id); // translate the page to current language
      }
    }

    Have a nice day,
    Lily

    Hello,

    I used the code of BowsDesign which worked perfectly. I am trying to add a filter for products listings now, but somehow doesn’t work as expected.

    add_filter('woocommerce_get_products_page_id', 'pll_woocommerce_get_products_page_id');
    function pll_woocommerce_get_products_page_id($id) {
                return pll_get_post($id); // translate the page to current language
            }

    I have a product page under /wp/en/products and a translation /wp/de/produkte
    When going to Cart and pressing back to Products I always end up on the /wp/en/products.

    Fix the problem by changing the

    add_filter('woocommerce_get_shop_page_id', 'pll_woocommerce_get_shop_page_id');
    function pll_woocommerce_get_shop_page_id($id) {
                return pll_get_post($id); // translate the page to current language
            }

    My problem now is that the products are shown only in /wp/en/products page, the translated page /wp/de/produkte doesn’t showing them. And in Woocommerce plugin I have only one page as option, in this case my products page. Is there any workaround for this problem?

    Thanks!

Viewing 10 replies - 16 through 25 (of 25 total)

The topic ‘Translating links’ is closed to new replies.