有没有办法避免编辑Magento Core(app/code/Core/Mage/Customer/controllers/AccountController.php(文件,以便在注册页面中启用客户组选项字段?
搜索它,唯一成功的方法是编辑前面提到的核心文件,找到以下代码:
$customer->getGroupId();
并将其替换为:
if($this->getRequest()->getPost('group_id'))
{ $customer->setGroupId($this->getRequest()->getPost('group_id'));
} else {
$customer->getGroupId();
}
最好的情况是,以某种方式,将此代码包含在本地模块文件中,类似于app/code/local/Enterprise/module/controllers/AccountController.php。我实际上有一个模块可以包含该函数,但仍然找不到要添加到模块上的正确代码。
始终建议为每个新功能创建一个新模块,但如果您只更改一行代码,您也可以将修改后的文件放在以下路径中(注意用local替换core(:app/code/local/Mage/Customer/controllers/AccountController.php
然后,你可以恢复你的核心文件,自动加载器仍然应该得到你修改过的版本。这样你就可以避免修改核心,但要记住,当你将Magento升级到新版本时,你可能也需要更新这个文件。
有关详细信息,请查看此链接:http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_create_a_local_copy_of_app_code_core_mage
要以正确的方式进行操作:在模块内扩展控制器。
将其放在config.xml中:
<frontend>
<routers>
<customer>
<args>
<modules>
<company_module before="Mage_Customer">Company_Module</company_module>
</modules>
</args>
</customer>
</routers>
</frontend>
然后,创建控制器文件app/code/local/Company/Module/controllers/AccountController.php
:
<?php
require_once 'Mage/Customer/controllers/AccountController.php';
class Company_Modulename_AccountController extends Mage_Customer_AccountController
{
/**
* Get Customer Model
*
* @return Mage_Customer_Model_Customer
*/
protected function _getCustomer()
{
$customer = $this->_getFromRegistry('current_customer');
if (!$customer) {
$customer = $this->_getModel('customer/customer')->setId(null);
}
if ($this->getRequest()->getParam('is_subscribed', false)) {
$customer->setIsSubscribed(1);
}
/**
* Initialize customer group id
*/
if($this->getRequest()->getPost('group_id')) {
$customer->setGroupId($this->getRequest()->getPost('group_id'));
} else {
$customer->getGroupId();
}
return $customer;
}
}