1

Im using jquery cookie to drop a cookie when the user presses a button on my mage store to show and hide vat prices.

My current js looks like so:

$j('.nav-customer-vat a').click(function(){

    $j(this).text($j(this).text() == 'Show VAT' ? 'Hide VAT' : 'Show VAT');
    $j(this).toggleClass('active');

    if($j(this).text() == 'Show VAT'){
        console.log('show');
        $j.cookie("showVat", false, { path: '/' });     
    } else if($j(this).text() == 'Hide VAT'){
        $j.cookie('showVat', true)
        console.log('hide');
    } 
}

Im then using on the frontend the following magento cookie getModel to test if the cookie is true or false:

<?php  $cookie = Mage::getModel('core/cookie')->get('showVat'); ?>
        <?php if($cookie){ ?>
        <a href="#" class="display-vat"><?php echo $this->__('Hide VAT'); ?></a>
        <?php } else { ?>
        <a href="#" class="display-vat"><?php echo $this->__('Show VAT'); ?></a>
    <?php } ?>

However this only seems to work once over then it will always return false on every click, I can't seem to figure why this is. Any help would be awesome.

andy jones
  • 904
  • 1
  • 12
  • 37

1 Answers1

1

It's because cookies are saved as string. And the string "false" evaluates as true. So you need to set actual string values and compare those.

More information in this post: jquery cookie set value to boolean true

Community
  • 1
  • 1
Sander Sluis
  • 160
  • 7
  • 1
    So just to clarify Aximus is right above, what i did to change this is the following: Altered the cookie to: $j.cookie('showVat', 'enabled', { path: '/' }); if($j(this).text() == 'Show VAT'){ $j.cookie("showVat", 'disable', { path: '/' }); } ' Then in php Thanks. – andy jones Jul 06 '16 at 08:07