• 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 15 replies - 1 through 15 (of 25 total)
  • Plugin Author Chouby

    (@chouby)

    It depends if woocommerce (or other plugins) is applying a filter when generating the link.

    When plugins use WordPress native function such as ‘get_permalink’, ‘get_category_link’ … this will work out of the box as Polylang of course filters these function.

    If the link is generated another way, then the plugin has to apply either one existing WordPress filter or its own filter. In that last case we need to know its name and parameters to add a Polylang filter.

    Thread Starter soylug

    (@soylug)

    First of all, thanks for your answer, nice support!
    This is what I could see:
    ‘cart_url’ => get_permalink( woocommerce_get_page_id( ‘cart’ ) )
    And the function is:
    ‘function woocommerce_get_page_id( $page ) {
    $page = apply_filters(‘woocommerce_get_’ . $page . ‘_page_id’, get_option(‘woocommerce_’ . $page . ‘_page_id’));
    return ( $page ) ? $page : -1;
    }
    }’
    Do you think it can be solved in an easy way? I’m sorry, I’m not a programmer πŸ™
    Thanks

    Plugin Author Chouby

    (@chouby)

    I’m sorry, I’m not a programmer πŸ™

    Not bad if you are not a programmer as you probably found what you need.

    I suggest that you create a custom plugin. Copy paste the code in texte file you name ‘something.php’. Upload the file in your plugins directory and activate the new plugin.

    <?php
    /*
    Plugin Name: Polylang Woocommerce compatibility
    */
    
    if (function_exists('pll_get_post')) { // is Polylang activated?
      add_filter('woocommerce_get_cart_page_id', 'pll_woocommerce_get_cart_page_id');
      function pll_woocommerce_get_cart_page_id($id) {
        return pll_get_post($id); // translate the page to current language
      }
    }

    This is of course not tested but hopefully it will do what you expect if you translated your cart page.

    Thread Starter soylug

    (@soylug)

    Perfect!! Thanks a lot!! I guess that I can do the same for other pages if I have the same issue.
    Thanks!! ^_^

    Thread Starter soylug

    (@soylug)

    Just one more question (hope last one). NextGEN Gallery plugin can link galleries to pages. When calling the link, it allways return the same page in the same language although there is a translation for it. More or less is the same than before, but the calling is:
    pageid); ?>”>
    Can I do something similar?
    Thanks!

    Plugin Author Chouby

    (@chouby)

    Yes you can use pll_get_post for any post, page, custom post. And there is pll_get_term for categories, post tags and terms in custom taxonomies.

    Thread Starter soylug

    (@soylug)

    Hi again! Your advices were very useful and I have solved many things doing what you told me, but there is a page which I can’t get working and don’t know why!
    It’s again about woocommerce plugin, for a page already translated. Is the same that I first asked but this time there is something different that I suppose is what is messing things.

    // filter hook for include new pages inside the payment method
    $get_checkout_url = apply_filters( 'woocommerce_get_checkout_url', $woocommerce->cart->get_checkout_url() ); ?>
    
    <form name="checkout" method="post" class="checkout" action="<?php echo esc_url( $get_checkout_url ); ?>">

    I’ve already added a filter as you said:

    add_filter('woocommerce_get_checkout_url', 'pll_get_checkout_url');
    function pll_get_checkout_url($link) {
        return pll_get_post($link);
    }

    but doesn’t work, this time it returns main language page. Then, if I change language (clicking on the flag) I get the page in the language selected, so they are linked but filter isn’t working.
    Thanks for your help!

    Plugin Author Chouby

    (@chouby)

    This can’t work because pll_get_post works with internal page number. And here your filter requires an url. Maybe you can post here the code of the function $woocommerce->cart->get_checkout_url() to see if we can do something with it?

    I see that you are trying to make Polylang and Woocommerce compatibles. I am sure this would interest a lot of people. Would you like to share the result of your work when it will be completed?

    Thread Starter soylug

    (@soylug)

    Thanks again!
    Here is the code for get_checkout_url:

    /**
    		 * Gets the url to the checkout page.
    		 *
    		 * @return string url to page
    		 */
    		public function get_checkout_url() {
    			$checkout_page_id = woocommerce_get_page_id('checkout');
    			$checkout_url     = '';
    			if ( $checkout_page_id ) {
    				if ( is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes' )
    					$checkout_url = str_replace( 'http:', 'https:', get_permalink( $checkout_page_id ) );
    				else
    					$checkout_url = get_permalink( $checkout_page_id );
    			}
    			return apply_filters( 'woocommerce_get_checkout_url', $checkout_url );
    		}

    I had already thought about sharing results (if I/we manage to get it working). As I said I’m not a programmer, so everything I try to change takes a lot of time for me πŸ™
    I also used it for nextgen plugin, which I guess is also interesting for more people. I’ll share, be sure πŸ˜‰

    Plugin Author Chouby

    (@chouby)

    So here we find the same function ‘woocommerce_get_page_id’ that we already filtered previously. So the solution is similar (I simplified it)

    <?php
    /*
    Plugin Name: Polylang Woocommerce compatibility
    */
    
    if (function_exists('pll_get_post')) { // is Polylang activated?
      add_filter('woocommerce_get_cart_page_id', 'pll_get_post');
      add_filter('woocommerce_get_checkout_page_id', 'pll_get_post');
    }

    You can do this each time you have: woocommerce_get_page_id('some_page')

    Thread Starter soylug

    (@soylug)

    Nop, that doesn’t work πŸ™
    It does for the cart page, but not for this one…

    Thread Starter soylug

    (@soylug)

    When I say it doesn’t work, I mean that redirects to the same page but appears white (doesn’t load). If I comment the “add_filter(‘woocommerce_get_checkout_page_id’, ‘pll_get_post’);” it returns the page in the main language.
    There is a plugin to get woocommerce working with wpml and I downloaded it to have a look in case I can find out something I could use, and it uses this function in the filter (for cart page, for example):

    function translate_pages_in_settings($id) {
    		return icl_object_id($id, 'page', true);
    	}

    I saw on your web that this is as pll_get_post. But for checkout page, their function is this one:

    function get_checkout_page_url(){
            return get_permalink(icl_object_id(get_option('woocommerce_checkout_page_id'), 'page', true));
        }

    I’m telling this just in case you can see a solution
    Thanks a lot

    Plugin Author Chouby

    (@chouby)

    The WPML version should work as is as Polylang includes some WPML function (including icl_object_id) πŸ™‚

    The pure Polylang version would look like:

    function get_checkout_page_url(){
      return get_permalink(pll_get_post(get_option('woocommerce_checkout_page_id')));
    }

    I guess there is somewhere something like

    add_filter('woocommerce_get_checkout_url', 'get_checkout_page_url');

    Thread Starter soylug

    (@soylug)

    I’m stuck with this πŸ™
    What I’m trying to do now is this:

    add_filter ('woocommerce_get_checkout_url', 'redirect_checkout_page_translated');
    function redirect_checkout_page_translated() {
    	$page_id = get_page_by_title('checkout');
    	$page_id_lan = pll_get_post ($page_id);
    	$url = get_permalink ($page_id_lan);
    	return $url;
      }

    but doesn’t work… If I don’t use pll_get_post, just this:

    add_filter ('woocommerce_get_checkout_url', 'redirect_checkout_page_translated');
    function redirect_checkout_page_translated() {
    	$page_id = get_page_by_title('checkout');
    	$url = get_permalink ($page_id);
    	return $url;
      }

    it redirects ok (I have try with several posts). I had understood that pll_get_post function works with ids, so it should work… or not?

    Plugin Author Chouby

    (@chouby)

    It’s strange that the second works as get_page_by_title does not return the page ID but a page object.
    So you should do:

    $page = get_page_by_title('checkout');
    $page_id_lan = pll_get_post ($page->ID);
    $url = get_permalink ($page_id_lan);

Viewing 15 replies - 1 through 15 (of 25 total)

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