问题
I need to check if visitor is first time on website and show block with country choice(stores). Then I need to save visitor choice in cookies(I just think that it is right to save it in cookies). Maybe someone did that and\or can help?
Thanks.
回答1:
When the user lands on the page check if a cookie is set, if it is then redirect to the store view based on the cookie value.
I'm pretty new to magento so there is probably a better way to do this, but i needed something similar myself.
I had a select box in a popup when the user landed on the page which listed all of the store views (this was in index.php and only shows when the cookie isn't set), on changing the value the form submitted, i then did the following at the bottom of the head.php file
if(isset($_POST['selectbox'])){
$storeId = $_POST['selectbox'];
$store_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).$storeId;
$cookie = Mage::getModel('core/cookie');
$period = time()+86400;
$cookie->set('country', $storeId,$period, '/');
Mage::app()->getResponse()->setRedirect($store_url);
}
So after the form submitted i grabbed the store code value and set a cookie named country, i then redirected the user to the specific store view.
I then did another check underneath this code to see if the country cookie was set (for next time the user visits the site)
if(isset($_COOKIE['country'])){
$storeId = $_COOKIE['country'];
$magento_store_id = Mage::app()->getStore()->getCode();
$redirect_to = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).$storeId;
if($storeId != $magento_store_id)
{
echo "<script type='text/javascript'>window.location.href='".$redirect_to."';</script>";
}
I checked to see if the current store code is not equal to the value stored in the cookie and if it isn't then redirect that user to the correct store view. I had to use the javascript redirect due to headers already output errors..
Hopefully that will give you some ideas and how to use the cookies..but like i said i imagine there's a better way of doing this. I'd be happy to see correct way of doing it myself to be honest so i can make use of it.
来源:https://stackoverflow.com/questions/12705087/magento-save-visitor-store-choice-in-cookies