解决magento中guest页面cookie保存时间过短问题
发布日期:2021-07-23 10:41:38 浏览次数:0 分类:技术文章

       在magento新的企业版11,有个新的功能退货(RMA),不仅能退货,还能以guest的身份查看订单的各种信息,但在IE下有个问题:cookies的保存时间太短了,如果需要使用到需要验证权限的操作,就会重定向到 sales/guest/form/ 页面。

       查看关键代码,发现跳转是发生在 Mage_Sales_Helper_Guest 类中


class Mage_Sales_Helper_Guest extends Mage_Core_Helper_Dat{    /**     * Cookie params     */    protected $_cookieName  = 'guest-view';    protected $_lifeTime    = 600;    /**     * Try to load valid order by $_POST or $_COOKIE     *     * @return bool|null     */    public function loadValidOrder()    {        if (Mage::getSingleton('customer/session')->isLoggedIn()) {            Mage::app()->getResponse()->setRedirect(Mage::getUrl('sales/order/history'));            return false;        }        $post = Mage::app()->getRequest()->getPost();        $type           = '';        $incrementId    = '';        $lastName       = '';        $email          = '';        $zip            = '';        $protectCode    = '';        $errors         = false;        /** @var $order Mage_Sales_Model_Order */        $order = Mage::getModel('sales/order');        if (empty($post) && !Mage::getSingleton('core/cookie')->get($this->_cookieName)) {            Mage::app()->getResponse()->setRedirect(Mage::getUrl('sales/guest/form'));            return false;        } elseif (!empty($post) && isset($post['oar_order_id']) && isset($post['oar_type']))  {            $type           = $post['oar_type'];            $incrementId    = $post['oar_order_id'];            $lastName       = $post['oar_billing_lastname'];            $email          = $post['oar_email'];            $zip            = $post['oar_zip'];            if (empty($incrementId) || empty($lastName) || empty($type) || (!in_array($type, array('email', 'zip')))                || ($type == 'email' && empty($email)) || ($type == 'zip' && empty($zip))) {                $errors = true;            }            if (!$errors) {                $order->loadByIncrementId($incrementId);            }            if ($order->getId()) {                $billingAddress = $order->getBillingAddress();                if ((strtolower($lastName) != strtolower($billingAddress->getLastname()))                    || ($type == 'email'                        && strtolower($email) != strtolower($billingAddress->getEmail()))                    || ($type == 'zip'                        && (strtolower($zip) != strtolower($billingAddress->getPostcode())))                ) {                    $errors = true;                }            } else {                $errors = true;            }            if (!$errors) {                $toCookie = base64_encode($order->getProtectCode());                Mage::getSingleton('core/cookie')->set($this->_cookieName, $toCookie, $this->_lifeTime, '/');            }        } elseif (Mage::getSingleton('core/cookie')->get($this->_cookieName)) {            $fromCookie     = Mage::getSingleton('core/cookie')->get($this->_cookieName);            $protectCode    = base64_decode($fromCookie);            if (!empty($protectCode)) {                $order->loadByAttribute('protect_code', $protectCode);                Mage::getSingleton('core/cookie')->renew($this->_cookieName, $this->_lifeTime, '/');            } else {                $errors = true;            }        }        if (!$errors && $order->getId()) {            Mage::register('current_order', $order);            return true;

   关键是:


 //如果没找到cookie的信息,就重定向到登录页面if (empty($post) && !Mage::getSingleton('core/cookie')->get($this->_cookieName)) {            Mage::app()->getResponse()->setRedirect(Mage::getUrl('sales/guest/form'));            return false

设置cookie的有效期是在:

                $toCookie = base64_encode($order->getProtectCode());                Mage::getSingleton('core/cookie')->set($this->_cookieName, $toCookie, $this->_lifeTime, '/');

从上面的代码可看出,$this->_lifeTime 的值就是cookie的有效期,于是解决方法就很简单了,rewrite这个helper,把 $_lifeTime 设置为一个比较大的值就行了。


【文章作者】曾健生

【作者邮箱】zengjiansheng1@126.com

【作者QQ】190678908

【作者博客】blog.csdn.net/newjueqi






上一篇:指定magento后台使用的theme
下一篇:magento中的启用https