3

I am in the process of trying to set a Magento cookie variable on click using jQuery but can't seem to get it too work.

Would the best way to do this be to hit a controller action using ajax when the click event is fired and then use the following code snippet inside the controller action?

Mage::getModel(‘core/cookie’)->set($name, $value);

Or is this approach a complex way of doing such a task and is there a different approach I should take?

4

I thought cookies could be set on the frontend?

Here is how you could store the store code using 'standard' Magento methods:

    var urlStoreCode = window.location.pathname.replace(/^\/+|\/+$/g,"").split('/')[0];
    if(urlStoreCode != "") Mage.Cookies.set('store', urlStoreCode);

That uses the Magento library and I don't see why jQuery is needed when there is an adequate library built in. Doing it this way means there is no need to create work for the server.

What did I miss?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.