24/7 Support: 800.608.6482


Try our new AI assistant by clicking the chat icon in the lower right corner.

Developer Videos

Videos | Working with Cookies

ver. 9.0 and later

Video Transcript

Cookies are a core part of the web and used in almost every website you visit. A cookie is just a small piece of data stored locally by your browser. It contains information about your web site. Such things as session ids, or if you’re logged in. However, cookies can be used for almost anything. It’s really a data storage tool for small pieces of data. You wouldn’t want to store a large amount of data in a cookie and there’s limits. However, say you wanted to track customers who came in from a certain url or landing page. On that landing page you could set a custom cookie and then read that in on the invoice or other pages to do something with it. This way you know where the customer came from and you can do something in real time. As a developer, Miva’s Template Language gives you access to both read existing cookies and set your own cookies. This allows you to do a lot of powerful things and gives you a lot of flexibility when building Miva Merchant stores. Let’s take a look how this is done.

First let’s take a look at how to set a custom cookie. To do this there’s actually one function we need. It’s called miva_output_header. Now this function is actually very powerful. It allows you to output any header with the web request. With http header, there’s a lot of predefined headers that you can use. One of them is called “set_cookie.” Because Miva gives us access to this function, we can output a header for set cookie and output our own custom cookie. Now, there’s a few key pieces to create a cookie. The main one is the name value pair for the cookie. So here I’m giving my cookie a name of mycookie and the value of that cookie is “hello world.” This value can be anything I want. Once I have my name_value_pair the second part is when the cookie expires. There’s a certain format to cookie expiration date and you can look this up, but it’s essentially a date time stamp of when you want the cookie to expire. The last part here is the path and the path is just a slash and this just means my cookies accessible at the root of the website. Now there are a few other cookie settings such as http only, secure, domain and you can use all these in the same Miva output header function. So once we have our cookies set, let’s take this and put it into Miva and see if it works. So here I am back in the Miva Merchant Admin. I’m going to go to the Storefront page and were going to paste in my code to set my cookie. So now let me open developer tools and then under resources I can view the cookies. So here there’s one cookie for the site and I’m going to refresh this page and it should send my custom cookie, which it does. So now we see mycookie is the name and then it has the value and then it has the main path and expiration. So this is a great way to create custom cookies.

So how about reading cookies. Say you have this cookie that’s set and you want to read its value. Well that’s also very easy. Kind of like post variables where Miva takes all post variables and get variables and turns them into global variables and does the same thing with cookies. It automatically reads the cookies on the site and will create global variables out of them. So if I jump back here to my text editor, Miva’s global variable for all cookies is called global: request_cookies:mycookie; and then the cookie name. So this variable will contain the mycookie variable. Now let’s say we didn’t know which cookies were available on the site. How do we find them? Well, we can actually use tokenlist. Let me jump back to the Miva Admin. And I’m going to pull up tokenlist and i’m going to search for g.request.cookies here and it will show me all the cookies that are on this site. So here’s mycookie, the one I created and it has the value of hello world. And here it will show you the entity to output this to the page. So Miva will automatically create global variables for every cookie on your site. So let me close this. Let’s paste in that value to see if it outputs to the page. I’ll jump back to the front end, hit “refresh” and you see here, there’s the value of the cookie “helloworld.” So you can use this method to grab any cookie that’s available on the site. Not only custom cookies you created, but maybe you want to grab the Miva basket id, which we’ll dive into how this is used a little bit later, or any other cookie that may be available. As long as you know the name of the cookie, you’ll be able to access it using this global variable.

Let’s jump back to our text editor and let me show you one last thing here. Say for example you wanted to iterate through all cookies that are available on the site and output them to the page or test for a certain cookie to see if it exists and do something if it does exist. Well, there’s a way we can do that natively through Miva. Now, this may look like a lot of code but let’s talk through it. As we were just talking about, there’s this global variable for request_cookies. This is a structure that contains every cookie that’s on the site. However, you can’t iterate through structures. You can’t use a while loop or a 4hloop to loop through all members of the structure. It just doesn’t work. Fortunately, Miva has a nice function called Miva Struct numbers. Which will take this structure and all it’s members and convert it to an array called g.cookies. So now I have a global variable which is an array which has the names of every cookie on the site. Because it’s an array we can use mvt:4each to iterate through each element of the array. So here is my global variable. I’m going to prefix the variable with global and then I’m going to give it an iterator of cookie. Now there’s one last thing we need to do. This array contains the names of all the cookies. This is because this function up here, this miva_struct_members converted the structure into an array or only the names or the numbers. So this means we have to do one other thing to get the value. There’s a function called miva_variable_value which takes in a string. So it’s the name of the variable as a string. So we know there’s a global variable with the name of g.request_cookies: and then the cookie name Well, we have a cookie name. It’s going to be whatever cookie name is in this iterator. So we can catenate that using the $ sign with l.settings_cookie. So that’s a string value that gets passed into this Miva variable value. What that function does is it takes that string value and evaluates that as if it was a global variable, which it is and saves it back into this variable. So we can see in our Developer Tools here, here’s the two cookies that are on the site and then here’s what our function did. It iterated through all the cookies and then outputs the name “mycookie” and “helloworld” and the same thing with the Miva basket id. So this is a great way to be able to iterate through all cookies on a site and do something if a cookie exists or has a certain value.

One last thing I want to show you is how to delete a cookie. So far we’ve learned how to set a cookie, how to read a specific cookie, and how to iterate through all cookies available. But what if you wanted to delete a cookie? Well to delete a cookie you actually use the same set cookie, you just set it with a date in the past. It doesn’t matter the date, just that it’s past the current date and time. Then browsers know to automatically delete that cookie. So let’s see, I’m going to take this and set this at 2013 and I’m going to copy the same code and let’s come back here to the admin and paste it. Now if I go to the Storefront and refresh you’ll see that the mycookie was automatically deleted by the browser.

Cookies are a great way to store small amounts of data about your shoppers. You can control them, set the expiration date and time so you can have a cookie stay a day, a week, 30 days, or even expire when the customer closes the browser. This is called a session cookie. Cookies are a great Developer Tool available at your disposal when building web sites using Miva Merchant.

Example Code

Looking for Developer Docs?

We have a whole section for that, including: Developer Training Series, Template Language docs, Module Development tutorials and much, much more.

Head to the Developer Section

This website uses cookies to identify visitors, track visitors to our website, store login session information and to remember your user preferences. By continuing to use this site you agree to our use of cookies. Learn More.

This website uses cookies. By continuing to use this site you agree to our use of cookies. Learn More.


Copyright © 1997 – 2024 Miva®, Miva Merchant®, MivaPay®, MivaCon®, Camp Miva®, Miva Connect®, Miva, Inc. All Rights Reserved.