在Cookie中保存访问者商店选择

在Cookie中保存访问者商店选择

本文介绍了Magento。在Cookie中保存访问者商店选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查访问者是否是第一次在网站上显示块与国家选择(商店)。然后我需要保存访问者的选择在cookie(我只是认为它是正确的保存在cookie)。也许有人这样做,并且可以帮助?

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?

感谢。

推荐答案

如果设置了cookie,则根据Cookie值重定向到商店视图。

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.

我对magento很新,所以可能有更好的方法

I'm pretty new to magento so there is probably a better way to do this, but i needed something similar myself.

当用户登录到列出了所有商店视图的页面时,我在弹出窗口中有一个选择框在index.php中,只有当cookie没有设置时才显示),在更改表单提交的值时,我然后在head.php文件的底部执行以下操作:

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.

然后我做了另一个检查下面的代码,看看是否设置国家cookie访问网站)

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>";
     }



我检查了当前商店代码是否不等于存储的值在cookie中,如果不是,那么将该用户重定向到正确的商店视图。我不得不使用javascript重定向,由于头已经输出错误..

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..

希望这会给你一些想法和如何使用cookies..but像我说想象有一个更好的方法这样做。我很高兴看到正确的做法,我自己诚实,所以我可以利用它。

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.

这篇关于Magento。在Cookie中保存访问者商店选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:22