最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin wpml - append currency to URL

programmeradmin2浏览0评论

I have a wordpress site with multilingual content (WPML) and multiple currencies.

What I want to achieve is that I can assign a specific currency through a URL and that that currency then stays "selected" until/unless overruled through the site's front-end currency selector. Those URLs I will use in email promotion to send the customer to the site in the currency that they need.

I have figured out how to add the currency through the URL (example URL = mysite/cart/?add-to-cart=123&wcmlc=EUR (where 123 is the product ID))

I have done this with the following code:

    add_action( 'wcml_client_currency', 'wcmlc' );
    function wcmlc( $current_currency ) {

        $wcmlc = isset( $_GET['wcmlc'] ) ? esc_attr( $_GET['wcmlc'] ) : $current_currency;

        return strtoupper( $wcmlc );
    }

This code works in that the customer arrives to the site with the currency set by the URL. BUT when the customer now starts browsing the website, the currency changes back to the default one...

I think a solution could be that the "wcml=EUR" part of the URL is made to stay appended to the URL but I have no idea how to do that.

I tried to do that with this piece of code which I found on this site (I am Looking to append URL Parameter to all URLs) but when I added this to my child-functions.php my site stopped working:

   function wprdcv_param_redirect(){
        if(isset($_COOKIE['wcmlc']) and !$_GET['wcmlc']){
            $location = esc_url(add_query_arg('wcmlc', $_COOKIE['wcmlc']));
            wp_redirect($location);
        }
    }
    add_action('template_redirect', 'wprdcv_param_redirect');

I don't know that appending the wcmlc onto the URL would be the solution (additionally when doing that it should still be allowed to be over-written by the front-end currency selector if the customer wishes to change the currency) or maybe an entirely different approach is needed.

If anyone can help me resolve this would be fantastic!

I have a wordpress site with multilingual content (WPML) and multiple currencies.

What I want to achieve is that I can assign a specific currency through a URL and that that currency then stays "selected" until/unless overruled through the site's front-end currency selector. Those URLs I will use in email promotion to send the customer to the site in the currency that they need.

I have figured out how to add the currency through the URL (example URL = mysite/cart/?add-to-cart=123&wcmlc=EUR (where 123 is the product ID))

I have done this with the following code:

    add_action( 'wcml_client_currency', 'wcmlc' );
    function wcmlc( $current_currency ) {

        $wcmlc = isset( $_GET['wcmlc'] ) ? esc_attr( $_GET['wcmlc'] ) : $current_currency;

        return strtoupper( $wcmlc );
    }

This code works in that the customer arrives to the site with the currency set by the URL. BUT when the customer now starts browsing the website, the currency changes back to the default one...

I think a solution could be that the "wcml=EUR" part of the URL is made to stay appended to the URL but I have no idea how to do that.

I tried to do that with this piece of code which I found on this site (I am Looking to append URL Parameter to all URLs) but when I added this to my child-functions.php my site stopped working:

   function wprdcv_param_redirect(){
        if(isset($_COOKIE['wcmlc']) and !$_GET['wcmlc']){
            $location = esc_url(add_query_arg('wcmlc', $_COOKIE['wcmlc']));
            wp_redirect($location);
        }
    }
    add_action('template_redirect', 'wprdcv_param_redirect');

I don't know that appending the wcmlc onto the URL would be the solution (additionally when doing that it should still be allowed to be over-written by the front-end currency selector if the customer wishes to change the currency) or maybe an entirely different approach is needed.

If anyone can help me resolve this would be fantastic!

Share Improve this question asked Jul 2, 2020 at 10:24 J.K.J.K. 32 bronze badges 2
  • If you want to do this I'd suggest looking at how the existing currency selector in the site does it. Maybe take a quick look at what happens in your browser when you change currency. If they don't change the URL then that information is being saved somewhere and you probably want to try changing that, rather than making a whole new URL system for every link on your site. The existing thing might save that setting in a cookie, or in Wordpress settings against the user. – mozboz Commented Jul 2, 2020 at 21:55
  • Thanks. I will try that. Though not really familiar where I would look or how to find it (but I guess that will be subject for a new ticket if I don't manage to find out. – J.K. Commented Aug 1, 2020 at 14:30
Add a comment  | 

1 Answer 1

Reset to default 0

Thanks to @mozboz I figured out the answer to my question. I am posting it here in case it helps somebody else:

Wordpress (WPML plugin for multi-language and multi-currency) uses the cookie _wcml_dashboard_currency. I was able to set a cookie with the following code and that handled my problem with the currency selection changing after browsing to a different page. Here the code:

    add_action( 'init', 'set_currency_cookie' );
    
    function set_currency_cookie() {
        if (isset($_GET['wcmlc'])) {
          $name = '_wcml_dashboard_currency';
          $id = $_GET['wcmlc'];    
           setcookie( $name, $id, time() + 0, "/", COOKIE_DOMAIN );
        }
    }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论