2011-08-09 55 views
1

我想添加一些自定義字段到註冊頁面。通過關於這個話題的各種帖子後,我試圖爲此創建一個模塊。我看到我的2個自定義字段被添加到eav_attribute和eav_entity_attribute表中。我還看到entity_type_id設置爲1,這是我的條目的客戶。但是,如果我在這些字段的註冊過程中添加任何數據,它不會保存到customer_entity_varchar表中。這裏是我的代碼: /etc/config.xml在magento 1.5中添加自定義註冊字段

<?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:

<?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:

<?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文件也顯示,我已經添加的字段。我需要添加什麼來將附加字段中的數據保存到數據庫並能夠檢索該數據?道歉在這裏粘貼代碼。謝謝。

回答

4

你也來註冊attribute_id新創建的屬性customer_form_attribute,所以Magento的知道那裏的東西:-)

在你的情況,你必須把這個在您的安裝文件:(採取形式的借記支付模塊例)

// 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(); 

希望這有助於:-)

+0

只是要清楚,我必須添加類似的代碼來註冊arrtibute_id到我Setup.php文件或mysql4安裝-0.1.0.php文件?謝謝。 – Nithin

+0

與安裝文件我的意思是mysql4-install-0.1.0.php :-) – tecmec

+0

謝謝了。這幫助了我。 – Nithin

相關問題