How To Use JavaScript To Pass A Cookie Value To Premium Web Cart?

by 4 replies
6
I'll keep this short...

I have an email address stored in a cookie on a Wordpress site.

I would like to pass that cookie value onto Premium Web Cart when the user clicks "add to cart" so they do not have to fill in the email address.

Premium Web Cart allows values to be passed using the URL. They allow the code email=whatever@anything.com to be added at the end of the add to cart url to pass that value to the the shopping cart.

What would the code be that converts the cookie to text, then adds it on to a pre-determined URL, then opens that URL with the email text added?

Any help would be greatly appreciated.
Tthanks
#programming #cart #cookie #javascript #pass #premium #premium web cart #web
  • is Premium Web Cart on the same domain as the wp site which has the cookie?

    I know I could do this for you, Just trying to figure out hte exact situation so I know which way to approach it.
  • To do this purely with javascript you need to put this function into the <HEAD> section of your page code:

    Code:
    <script>
        function getCookie(c_name)
    {
    var i,x,y,ARRcookies=document.cookie.split(';');
    for (i=0;i<ARRcookies.length;i++)
    {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf('='));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf('=')+1);
      x=x.replace(/^s+|s+$/g,'');
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    </script>
    And then the actual link would look like this...

    Code:
    <a href="http://www.shoppingcartpage.com" onclick="this.href=this.href+'?email='+getCookie('email_cookie_name');">Add To Cart</a>
    Hope that helps!

    -Ryan
    • [ 1 ] Thanks
    • [1] reply
    • Thanks Ryan, but I'm getting the email showing up as "undefined".
      I've checked the browser cookies. The email cookie is there.

      I tried the code above changing 'email_cookie_name' to email2 (the cookie name). Also tried it changing c_name in all spots to email2. Also tried it without.

      Am I doing something wrong?
  • If you deletes the cookies it will disappear.

Next Topics on Trending Feed