1

我尝试在 magento 中注册全局变量,但它不起作用。在 /app/code/core/Mage/Checkout/controllers/CartController.php 我注册变量:

Mage::register('g_addressType', $addressType);

$mylog = print_r(Mage::registry('g_addressType'), true);
Mage::log("address_type1:".$mylog, null, 'mygento.log');

$this->_redirect('checkout/onepage/');

在 /app/code/core/Mage/Checkout/Block/Onepage.php 我尝试获取此变量:

$mylog = print_r(Mage::registry('g_addressType'), true);
Mage::log("address_type2:".$mylog, null, 'mygento.log');
return Mage::registry('g_addressType');

但它不起作用。我得到这个日志:

2013-06-04T13:38:45+00:00 DEBUG (7): address_type1:private
2013-06-04T13:38:51+00:00 DEBUG (7): address_type2:

错误在哪里?我使用magento 1.7。是的,我知道我无法更改核心文件。谢谢。

4

1 回答 1

2

这不起作用,因为 Magento 注册表在页面加载时不是持久的。从 CartController (/checkout/cart/) 更改为 OnepageController (/checkout/onepage/) 意味着重新加载页面,因此您的 g_addressType 不会保留在两者之间。

更好的做法是使用这样的会话:

Mage::getSingleton('checkout/session')->setGAddressType($addressType)在 CartController.php 中

然后

Mage::getSingleton('checkout/session')->getGAddressType()在 OnepageController.php

最后提醒一下:请确保不要在 Core Magento 文件中对此进行编码。请扩展现有的核心课程。

于 2013-06-04T14:53:35.643 回答