在magento 1.5中添加自定义注册字段 [英] Adding custom registration fields in magento 1.5

查看:53
本文介绍了在magento 1.5中添加自定义注册字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向注册页面添加几个自定义字段.浏览了有关该主题的各种文章之后,我尝试为此创建一个模块.我看到我的2个自定义字段已添加到eav_attribute和eav_entity_attribute表中.我还看到entity_type_id设置为1,这是我输入的客户.但是,如果我在注册这些字段时添加任何数据,则不会将其保存到表customer_entity_varchar中.这是我的代码: /etc/config.xml

i am trying to add a couple of custom fields to the registration page. After going though various posts about this topic i tried to create a module for this. I see that my 2 custom fields are added to the eav_attribute and eav_entity_attribute tables. I also see that the entity_type_id is set to 1 which is customer for my entries. But if i add any data during registration for these fields its not saved to the table customer_entity_varchar . Here is my code: /etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Foostor_Extcustomerfields>
            <version>0.1.0</version>
        </Foostor_Extcustomerfields>
    </modules>
<!--    <frontend>  
        <routers>
            <pws_extshipping>
                <use>standard</use>
                <args>
                    <module>PWS_ExtShipping</module>
                    <frontName>pws_extshipping</frontName>
                </args>
            </pws_extshipping>
        </routers>      
        <layout>
            <updates>
                <pws_extcustomerfields module="PWS_ExtCustomerFields">
                    <file>pws_extcustomerfields.xml</file>
                </pws_extcustomerfields>
            </updates>
        </layout>        
    </frontend>  -->
    <global> 
        <models>
            <foostor_extcustomerfields>
                <class>Foostor_Extcustomerfields_Model</class>
            </foostor_extcustomerfields>
        </models>
        <resources>
            <foostor_extcustomerfields_setup>
                <setup>
                    <module>Foostor_Extcustomerfields</module>                    
                    <class>Foostor_Extcustomerfields_Model_Entity_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </foostor_extcustomerfields_setup>
            <foostor_extcustomerfields_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </foostor_extcustomerfields_write>
            <foostor_extcustomerfields_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </foostor_extcustomerfields_read>     
        </resources>
        <blocks>     
            <foostor_extcustomerfields>
                <class>Foostor_Extcustomerfields_Block</class>
            </foostor_extcustomerfields>    
        </blocks>       
        <helpers>
            <foostor_extcustomerfields>
                <class>Foostor_Extcustomerfields_Helper</class>
            </foostor_extcustomerfields>            
        </helpers>
        <fieldsets>
            <customer_account> 
                <registration_number><create>1</create><update>1</update></registration_number>
                <registration_comment><create>1</create><update>1</update></registration_comment>
            </customer_account>
        </fieldsets>  
    </global>    
</config>

/Model/Entity/Setup.php:

/Model/Entity/Setup.php:

<?php
class Foostor_Extcustomerfields_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup
{


    public function getDefaultEntities()
    {

        $defaultEntities = parent::getDefaultEntities();

        $defaultEntities['customer']['attributes']['registration_number'] = array(
                        'label'        => 'Company Registration Number',
                        'visible'      => 1,
                        'required'     => 1,
                        'position'     => 1,
                    );              

        $defaultEntities['customer']['attributes']['registration_comment'] = array(
                        'label'        => 'Comment',
                        'visible'      => 1,
                        'input'        => 'textarea',
                        'required'     => 1,
                        'position'     => 1,
                    );                          

        return $defaultEntities;
    }

}

/sql/foostor_extcustomerfields_setup/mysql4-install-0.1.0.php:

/sql/foostor_extcustomerfields_setup/mysql4-install-0.1.0.php:

<?php
$installer = $this;
/* @var $installer PWS_ExtCustomerFields_Model_Entity_Setup */

$installer->startSetup();

$installer->addAttribute('customer', 'registration_number', array(
    'label'        => 'Company Registration Number',
    'visible'      => 1,
    'required'     => 1,
    'position'     => 1,
));

$installer->addAttribute('customer', 'registration_comment', array(
    'label'        => 'Comment',
    'visible'      => 1,
    'input'        => 'textarea',
    'required'     => 0,
    'position'     => 1,
));


$installer->endSetup();

我也已将所需的代码添加到register.phtml和edit.phtml文件中,以显示我添加的字段.我需要添加什么才能将其他字段中的数据保存到数据库中并能够检索该数据?抱歉在这里粘贴代码.谢谢.

I have added the required code to the register.phtml and edit.phtml file too to display the fields that i have added. What do i need to add to save the data from the additional fields to the database and be able to retrieve that data? Apologize for pasting the code here. Thanks.

推荐答案

您还必须将新创建的属性的 attribute_id 注册到 customer_form_attribute ,因此Magento知道在哪里事情是:-)

You have also to register the attribute_id of new created attributes to the customer_form_attribute, so Magento knows where things are :-)

在这种情况下,您必须将其放入安装文件中:(示例来自借记付款模块)

In your case you have to put this in your setup file: (Example taken form the Debit Payment Module)

// Get Customer Type ID
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$eid = $read->fetchRow(
    "select entity_type_id from {$this->getTable('eav_entity_type')} where entity_type_code = 'customer'"
);
$customer_type_id = $eid['entity_type_id'];

// Save Attribute to the customer_form_attribute
$attribute = $eavConfig->getAttribute($customer_type_id, 'registration_number');

// Here is where you determine in wich areas of magento the attributes are used
$attribute->setData('used_in_forms', array('customer_account_edit', 'customer_account_create', 'adminhtml_customer'));
$attribute->save();

希望这会有所帮助:-)

Hope this helps :-)

这篇关于在magento 1.5中添加自定义注册字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆