COOKIES: Cookie Privacy/Consent Message and Button and JS

Miva Knowledge Base
Want to start an online store? We work with you from start to finish, from commerce platform to design to SEO.
Experience counts, and we have a lot.
Important Notice: This information is for internal reference only. Use at your own risk.

COOKIES: Cookie Privacy/Consent Message and Button and JS

Scot Ranney • March 07, 2024


May 15 2025 - Shadows

Brave and other browsers have now implemented a maximum one week cookie lifespan for JS created cookies.

To get around this we will now use an AJAX call to set the cookie using the miva merchant cookie API functions to set a max life cookie, which is six months.

Requirements:

1. Make sure the htaccess file is set to force www (or whatever) and https. Differences in the domain will make the browser think it's a different session and the cookies won't work.

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

Setup:

AJAX PAGE: Open or create an ajax page template.

Name: AJAX Handler
Code: AJAX
Url: /ajax-handler.html
Page Items: STORE (possibly not needed, not quite sure)

Force HTTPS should be on.

Template Code:

<mvt:comment>
#
# code to create a six month cookie (max allowed by some browsers.) 
#
</mvt:comment>

<mvt:if expr="g.ajax_action EQ 'set_privacy_cookie'">
	<mvt:assign name="l.cookie" value="''" />
	<mvt:assign name="l.cookie:name" value="'cookieconsent'" />
	<mvt:assign name="l.cookie:value" value="s.dyn_time_t" />
	<mvt:assign name="l.cookie:domain" value="''" />
	<mvt:assign name="l.cookie:expires" value="s.dyn_time_t + (86400 * 365)" />
	<mvt:assign name="l.cookie:path" value="'/'" />
	<mvt:assign name="l.cookie:secure" value="'1'" />
	<mvt:assign name="l.cookie:httponly" value="'1'" />
	<mvt:assign name="l.cookie:samesite" value="'Strict'" />
	<mvt:assign name="l.ok" value="miva_array_insert(l.settings:cookies,l.cookie,-1)" />
	<mvt:do file="g.Module_Library_Utilities" name="l.exp" value="Format_RFC1123_DateTime( l.cookie:expires )" />
	<mvt:do file="g.Module_Library_Utilities" name="l.success" value="v10_SetCookie(l.settings:cookies, l.cookie:name, l.cookie:value, l.cookie:domain, l.cookie:expires, l.cookie:path, l.cookie:secure, l.cookie:samesite)" />
	<mvt:do file="g.Module_Library_Utilities" name="l.success" value="OutputCookies(l.settings:cookies)" />
</mvt:if>

CONTENT SECTION: 

Content Section Code: cookie_privacy_message
Content Section Name: Cookie Privacy Message

The cookie below is set for a six month expiration.

<style>
#cookie-message {
   position: fixed;
   bottom: 0;
   width: 100%;
   z-index:2000;
}
</style>

<mvt:if expr="NOT g.request_cookies:cookieconsent">
   <div id="cookie-message" class="o-layout o-layout--justify-around u-bg-gray-50 u-text-gray-10" style="padding: 3% 8%; font-weight:700; width:105%!important">
	  <div class="u-width-12 u-width-8--m  u-color-gray-30">
		 By continuing to browse, you accept our use of cookies as explained in our <a href="/privacy-policy.html" target="_self">Privacy Policy</a>.<p></p>
	  </div>
	  <div class="u-width-12 u-width-4--m u-text-center" style="display:flex; align-items:center;justify-content: center">
		 <button id="cookie-set" class="c-button  u-bg-blue u-color-white  u-font-small u-text-uppercase">Click Here to Accept Cookies</button>
	  </div>
   </div>
	<script>
   	$("#cookie-set").click(function(){
		$.ajax({
			url: '/ajax-handler.html',
			data: { 'ajax_action':'set_privacy_cookie' },
			type: 'POST',
			success: function (result) {						 
				$('#cookie-message').fadeOut();
			}
		});
  	 });
	</script>
</mvt:if>

Bottom of global footer:

<mvt:comment>
#
# cookie message - can be edited in the readytheme content section area
#
</mvt:comment>

<mvt:item name="readytheme" param="contentsection( 'cookie_privacy_message' )" />

https://www.scotsscripts.com/mvblog/cookies-cookie-consent-message-and-button-and-js.html

mvkb_cookies mvkb_js mvkb_shadows mvkb_privacy mvkb_consent