I am trying to set a default value of "false" when someone first enters the site. Then once a button is clicked, it will update. This works just fine. The issue though is when they refresh or go back to the page, it will reset back to "false" no matter what. I already have all cookies set to expire after a year. I'm not sure what I am doing wrong?
require(['jquery', 'jquery/ui','Magento_Ui/js/modal/modal'], function($){
jQuery(document).ready( function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
var pairs = document.cookie.split(";");
var cookies = {};
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
cookies[(pair[0] + '').trim()] = unescape(pair.slice(1).join('='));
}
let check_cookie = cookies.cookie_consent;
if (check_cookie !== null && check_cookie !== undefined) {
let checkCookies = check_cookie.split(",");
for (var i = 0; i < checkCookies.length; i++) {
let cookieTags = checkCookies[i];
let tagName = cookieTags.substring(0, cookieTags.indexOf('='));
let tagValue = cookieTags.substr(cookieTags.indexOf("=") + 1);
let tag = "#"+ tagName;
if(tagValue === 'true'){
jQuery(tag).prop('checked', true);
} else {
jQuery(tag).prop('checked', false);
}
}
}
$('.open-model-button').click(function () {
$('#popup-modal').modal('openModal');
$('.modal-footer').hide();
});
});
});
require(['jquery', 'mage/cookies'], function ($) {
jQuery.cookie('rlx-consent', 'false', { expires: 365, path: '/', domain: "davidfairclough.com" });
});
Then I also have the code for each button clicked
function accept() {
jQuery.cookie('rlx-consent', 'true', { expires: 365, path: '/', domain: "davidfairclough.com" });
}
function acceptSelected() {
let google_analytics = document.getElementsByName('google_analytics');
let gtm = document.getElementsByName('gtm');
let rolex = document.getElementsByName('rolex');
let cookieCmp = 'google_analytics=' + google_analytics[0].checked + "%" + 'gtm=' + gtm[0].checked + "%" + 'rolex=' + rolex[0].checked;
jQuery.cookie('cookie_consent', cookieCmp, { expires: 365, path: '/', domain: "davidfairclough.com" });
jQuery.cookie('rlx-consent', 'true', { expires: 365, path: '/', domain: "davidfairclough.com" });
}
function decline() {
jQuery.cookie('rlx-consent', 'false', { expires: 365, path: '/', domain: "davidfairclough.com" });
}
Test whether the cookie already exists before initializing it.
if (!jQuery.cookie('rlx-consent')) {
jQuery.cookie('rlx-consent', 'false', { expires: 365, path: '/', domain: "davidfairclough.com" });
}