How to use Cookie with Magento?

Some time developer gets stuck with how to pass variable value from PHP to JavaScript (server side to client side). We can found a solution with cookie. Because cookie is the feature which is available at both side (PHP & JavaScript / Server side & Client side).

In Magento it has in-built JS library for cookie that his js\mage\cookies.js and for php side it has cookie model (core/cookie). So in magento we do not need to write core function for cookie.

 

How to use cookie in JS?

Set cookie value.

Mage.Cookies.set(‘somename’,’somevalue’);

Get Cookie value

Mage.Cookies.get(‘somename’);

Delete Cookie / Cleare Cookie

Mage.Cookies.clear(‘somename’);

 

How to use cookie with magento model?

Set Cookie value.

Mage::getModel(‘core/cookie’)->set(‘somename’,’somevalue’);

Get Cookie Value.

Mage::getModel(‘core/cookie’)->get(‘somename’);

Delete Cookie

Mage::getModel(‘core/cookie’)->delete(‘somename’);