cookies – $_COOKIE isset issue with WooCommerce and Firefox

I have a custom cookie in my WooCommerce store where the visitor can switch between guest roles “Private” and “Company” to show some different design and prices depending on the active role.

After I switch the role to “Company” and the cookie is updated accordingly, I browse around a couple of subpages. !isset($_COOKIE(‘pkuserrole’)) is then triggered even though I have an existing cookie. This would trigger the role to “private” as standard. In Chrome or Edge it works fine despite this, and I can finish an order as “Company” without the roles being switched after the Woo thank you page. But in Firefox the Woo thank you page updates the cookie variable from “company” to “private” and switches me back to “Private”‘s design and content.

I noticed my ‘init’ action is executed maybe 5 – 7 times per page load but the issue is still there when I tried to load the function with ‘template_redirect’ which was only executed once per page load.

I’m thinking maybe it could have something to do with sent headers issues or maybe the time in the cookie being WP’s standard UTC format and my computer timezone being GMT+1 which gets a 2hr difference?

// Get user role from already existing cookie
function getCookie(){
    global $selectedUserRole;

    if( isset($_COOKIE('pkuserrole')) ){

        // Cookie is already set, get its contents
        if( $_COOKIE('pkuserrole')  == 'private' ){
            $selectedUserRole = 'private';
        }
        else if( $_COOKIE('pkuserrole')  == 'company' ){
            $selectedUserRole = 'company';
        }
        changeRoleVariables($selectedUserRole);
    }
}

function placeCookie($selectedUserRole){
    global $selectedUserRole;

    $time = time();
    $timePlusOneMonth = $time+2592000;
    $timeMinusOneMonth = $time-2592000;

    if( isset($_COOKIE('pkuserrole')) ) {
        // Cookie is already set, reset it with new value
        unset($_COOKIE('pkuserrole'));

        setcookie("pkuserrole", $selectedUserRole, (
            'expires' => $timePlusOneMonth,
            'path' => COOKIEPATH,
            'domain' => COOKIE_DOMAIN,
            'secure' => true,
            'httponly' => false,
            'samesite' => 'Strict',
        ));
    }
    else if( !isset($_COOKIE('pkuserrole')) ){
        // Cookie is not set, place a new cookie
        setcookie("pkuserrole", $selectedUserRole, (
            'expires' => $timePlusOneMonth,
            'path' => COOKIEPATH,
            'domain' => COOKIE_DOMAIN,
            'secure' => true,
            'httponly' => false,
            'samesite' => 'Strict',
        ));
    }
}

 function controlUserRole_notajax(){
    global $selectedUserRole;

    // Logged in customer?
    if( is_user_logged_in() && !current_user_can( 'administrator' ) ){
        $user = wp_get_current_user();

        if ( in_array( 'company', (array) $user->roles ) ){
            $selectedUserRole = 'company';

            // Updates cookie with $selectedUserRole company
                placeCookie($selectedUserRole);

            // Changes variables with $selectedUserRole company
                changeRoleVariables($selectedUserRole);
        }
        else{
            $selectedUserRole = 'private';

            // Updates cookie with $selectedUserRole private
                placeCookie($selectedUserRole);

            // Changes variables with $selectedUserRole private
                changeRoleVariables($selectedUserRole);
        }
    }
    else{ 
        // Controls if a user role is checked in the header or in modal
        if( !empty($_POST("selectedUserRole")) ) {

            if( $_POST("selectedUserRole") == 'private' ){
                // "Private is checked"
                $selectedUserRole = 'private';

                placeCookie($selectedUserRole);
                changeRoleVariables($selectedUserRole);
            }
            
            else if( $_POST("selectedUserRole") == 'company' ){
                // If "Company is checked"
                $selectedUserRole = 'company';

                placeCookie($selectedUserRole);
                changeRoleVariables($selectedUserRole);
            }
        } // Not logged in and no role checked, checks if cookie exists or not.
        else if( empty($_POST("selectedUserRole")) && isset($_COOKIE('pkuserrole')) ){
            getCookie();
        }
        else if( empty($_POST("selectedUserRole")) && !isset($_COOKIE('pkuserrole')) ){
            // No cookie is set, push 'private' into cookie as standard

            $selectedUserRole = 'private';
            placeCookie($selectedUserRole);
            changeRoleVariables($selectedUserRole);
        }
    }
}
add_action('init', 'controlUserRole_notajax');