Reputation: 922
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.
Upvotes: 1
Views: 375
Reputation: 170
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
Upvotes: 1