I am working on a wordpress website with woocommerce. In my cart.php I defined a javascript cookie as I want to store some choices that a user does on this specific page. The code is

    jQuery(document).ready(function($) {
    $("input[name=\"license-selector\"]").click(function(){ // when license-selector radio button is being clicked
        var thisElem = $(this);                             // value is being written to variable value
        var value = thisElem.val();
        sessionStorage.setItem("option", value);            // value stored in localstorage
        document.cookie="option="+value;                    // value stored in cookie
    var itemValue = sessionStorage.getItem("option");       // value is being loaded into variable itemValue

    var n = document.cookie;
    if (n.indexOf("option=") !== -1) {
        var cookieValue = n.substring(n.indexOf("option=")+7, n.indexOf(";"))   // value is being loaded into variable cookieValue
        if (cookieValue == "su") {single_user()};
        if (cookieValue == "mu") {multi_user()};

So, the cookie gets the value of a radio button that is being clicked. So far so good. The script is called from an external script file that is applied ONLY for the cart page (cart.php of woocommerce). Now, what I try to do is to delete this cookie whenever the user navigates away from this cart page (not from the website, but whenever the user decides to visit for example the products, the blog, the homepage etc of the same website I am working on). I want to delete the cookie and have the visitor make the choice again when they revisit the cart page. So... they are in cart page, make their selection, cookie is stored... then they visit for example the shop page (or any other page of the website), the cookie gets deleted, and when they revisit the cart page (same session), they have to make select again (thus setting the cookie again). In order to delete the cookie, I 've inserted temporarily this script in the header.php of my theme (all these in child theme).

    // Delete cookie
// store url on load

var currentPage = window.location.href;
var cookiePage = 'http://localhost/my-site/cart/';

// listen for changes
    if (currentPage != cookiePage)

        document.cookie = "option=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
        var cookieValue = 0;
}, 500);

As you correctly guessed... it doesn't wooork.... Any help to make it work will be hugely appreciated. The goal is to delete the cookie when the visitor leaves the cart page. Btw, I know there is the solution of onbeforeunload and call a function to empty the cookie but I don't want this solution. Using onbeforeunload deletes the cookie whenever I click on "update cart" in the cart page and I don't want this (the cart page gets updated via ajax when I click on "update cart").

Thanks again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.