2011-07-16 34 views
12

Trong hai ngày, tôi đã cố gắng thêm thuộc tính khách hàng mới vào cơ sở dữ liệu Magento. Nhưng nó không hoạt động. Tôi đã thêm một đơn giản "nickname" trường nhập đến register.phtmledit.phtml làm theo hướng dẫn này:Magento - Thêm thuộc tính vào thực thể của khách hàng

http://www.magentocommerce.com/wiki/5_-_modules_and_development/customers_and_accounts/registration_fields

Dưới đây là các bài diễn đàn cố hữu:

http://www.magentocommerce.com/boards/viewthread/9620/

Thuộc tính được thêm vào bảng cơ sở dữ liệu eav_attribute, nhưng giá trị không ap lê trong customer_entity_varchar.


Dưới đây là những gì tôi có:

Tất cả các file đều nằm trong app/code/local/Công ty/


app/code/local/Công ty/khách hàng/etc/config.xml

http://mysticpaste.com/view/9466 (xem ra: <!-- nickname -->)

app/code/local/Công ty/khách hàng/mẫu/Entity/setup.php (coi chừng: /* nickname */)

http://mysticpaste.com/view/9467

 
class Company_Customer_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup{ 

    public function getDefaultEntities() 
     { 
      return array(
       'customer' => array(
        'entity_model'   =>'customer/customer', 
        'table'     => 'customer/entity', 
        'increment_model'  => 'eav/entity_increment_numeric', 
        'increment_per_store' => false, 
        'additional_attribute_table' => 'customer/eav_attribute', 
        'entity_attribute_collection' => 'customer/attribute_collection', 
        'attributes' => array(
    //     'entity_id'   => array('type'=>'static'), 
    //     'entity_type_id' => array('type'=>'static'), 
    //     'attribute_set_id' => array('type'=>'static'), 
    //     'increment_id'  => array('type'=>'static'), 
    //     'created_at'  => array('type'=>'static'), 
    //     'updated_at'  => array('type'=>'static'), 
    //     'is_active'   => array('type'=>'static'), 

         'website_id' => array(
          'type'   => 'static', 
          'label'   => 'Associate to Website', 
          'input'   => 'select', 
          'source'  => 'customer/customer_attribute_source_website', 
          'backend'  => 'customer/customer_attribute_backend_website', 
          'sort_order' => 10, 
         ), 
         'store_id' => array(
          'type'   => 'static', 
          'label'   => 'Create In', 
          'input'   => 'select', 
          'source'  => 'customer/customer_attribute_source_store', 
          'backend'  => 'customer/customer_attribute_backend_store', 
          'visible'  => false, 
          'sort_order' => 20, 
         ), 
         'created_in' => array(
          'type'   => 'varchar', 
          'label'   => 'Created From', 
          'sort_order' => 30, 
         ), 
         'prefix' => array(
          'label'   => 'Prefix', 
          'required'  => false, 
          'sort_order' => 37, 
         ), 
         'firstname' => array(
          'label'   => 'First Name', 
          'sort_order' => 40, 
         ), 
         'middlename' => array(
          'label'   => 'Middle Name/Initial', 
          'required'  => false, 
          'sort_order' => 43, 
         ), 
         'lastname' => array(
          'label'   => 'Last Name', 
          'sort_order' => 50, 
         ), 
         'suffix' => array(
          'label'   => 'Suffix', 
          'required'  => false, 
          'sort_order' => 53, 
         ), 
         'email' => array(
          'type'   => 'static', 
          'label'   => 'Email', 
          'class'   => 'validate-email', 
          'sort_order' => 60, 
         ), /* nickname */ 
         'nickname' => array(
          'label'   => 'nickname', 
          'unique'  => true, 
          'sort_order' => 65, 
          'required'  => true, 
         ), 
         'group_id' => array(
          'type'   => 'static', 
          'input'   => 'select', 
          'label'   => 'Group', 
          'source'  => 'customer/customer_attribute_source_group', 
          'sort_order' => 70, 
         ), 
         'dob' => array(
          'type'   => 'datetime', 
          'input'   => 'date', 
          'backend'  => 'eav/entity_attribute_backend_datetime', 
          'required'  => false, 
          'label'   => 'Date Of Birth', 
          'sort_order' => 80, 
         ), 
         'password_hash' => array(
          'input'   => 'hidden', 
          'backend'  => 'customer/customer_attribute_backend_password', 
          'required'  => false, 
         ), 
         'default_billing' => array(
          'type'   => 'int', 
          'visible'  => false, 
          'required'  => false, 
          'backend'  => 'customer/customer_attribute_backend_billing', 
         ), 
         'default_shipping' => array(
          'type'   => 'int', 
          'visible'  => false, 
          'required'  => false, 
          'backend'  => 'customer/customer_attribute_backend_shipping', 
         ), 
         'taxvat' => array(
          'label'   => 'Tax/VAT Number', 
          'visible'  => true, 
          'required'  => false, 
         ), 
         'confirmation' => array(
          'label'   => 'Is Confirmed', 
          'visible'  => false, 
          'required'  => false, 
         ), 
         'created_at' => array(
          'type'   => 'static', 
          'label'   => 'Created At', 
          'visible'  => false, 
          'required'  => false, 
          'input'   => 'date', 
         ), 
        ), 
       ), 

       'customer_address'=>array(
        'entity_model' =>'customer/customer_address', 
        'table' => 'customer/address_entity', 
        'additional_attribute_table' => 'customer/eav_attribute', 
        'entity_attribute_collection' => 'customer/address_attribute_collection', 
        'attributes' => array(
    //     'entity_id'   => array('type'=>'static'), 
    //     'entity_type_id' => array('type'=>'static'), 
    //     'attribute_set_id' => array('type'=>'static'), 
    //     'increment_id'  => array('type'=>'static'), 
    //     'parent_id'   => array('type'=>'static'), 
    //     'created_at'  => array('type'=>'static'), 
    //     'updated_at'  => array('type'=>'static'), 
    //     'is_active'   => array('type'=>'static'), 

         'prefix' => array(
          'label'   => 'Prefix', 
          'required'  => false, 
          'sort_order' => 7, 
         ), 
         'firstname' => array(
          'label'   => 'First Name', 
          'sort_order' => 10, 
         ), 
         'middlename' => array(
          'label'   => 'Middle Name/Initial', 
          'required'  => false, 
          'sort_order' => 13, 
         ), 
         'lastname' => array(
          'label'   => 'Last Name', 
          'sort_order' => 20, 
         ), 
         'suffix' => array(
          'label'   => 'Suffix', 
          'required'  => false, 
          'sort_order' => 23, 
         ), 
         'company' => array(
          'label'   => 'Company', 
          'required'  => false, 
          'sort_order' => 30, 
         ), 
         'street' => array(
          'type'   => 'text', 
          'backend'  => 'customer_entity/address_attribute_backend_street', 
          'input'   => 'multiline', 
          'label'   => 'Street Address', 
          'sort_order' => 40, 
         ), 
         'city' => array(
          'label'   => 'City', 
          'sort_order' => 50, 
         ), 
         'country_id' => array(
          'type'   => 'varchar', 
          'input'   => 'select', 
          'label'   => 'Country', 
          'class'   => 'countries', 
          'source'  => 'customer_entity/address_attribute_source_country', 
          'sort_order' => 60, 
         ), 
         'region' => array(
          'backend'  => 'customer_entity/address_attribute_backend_region', 
          'label'   => 'State/Province', 
          'class'   => 'regions', 
          'sort_order' => 70, 
         ), 
         'region_id' => array(
          'type'   => 'int', 
          'input'   => 'hidden', 
          'source'  => 'customer_entity/address_attribute_source_region', 
          'required'  => 'false', 
          'sort_order' => 80, 
          'label'   => 'State/Province' 
         ), 
         'postcode' => array(
          'label'   => 'Zip/Postal Code', 
          'sort_order' => 90, 
         ), 
         'telephone' => array(
          'label'   => 'Telephone', 
          'sort_order' => 100, 
         ), 
         'fax' => array(
          'label'   => 'Fax', 
          'required'  => false, 
          'sort_order' => 110, 
         ), 
        ), 
       ), 
      ); 
     } 
} 

ứng dụng/thiết kế/frontend/default /company/template/customer/form/register.phtml

<label for="nickname" class="required" style="margin-left: -1px;"><em>*</em><?php echo $this->__('Nickname') ?></label> 
<input type="text" name="nickname" id="nickname" value="<?php echo $this->htmlEscape($this->getFormData()->getnickname()) ?>" title="<?php echo $this->__('Nickname') ?>" class="input-text required-entry" /> 

tôi cũng chạy p IECE mã php ở phía trên cùng của register.phtml để thêm thuộc tính đến eav_attribute mã này được thực hiện một lần:

<?php 
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
    $AttrCode = 'nickname'; 
    $settings = array (
     'position' => 1, 
     'is_required' => 0 
    ); 
    $setup->addAttribute('1', $AttrCode, $settings); 
?> 

ứng dụng/thiết kế/frontend/default/công ty/mẫu/khách hàng /form/edit.phtml

<label for="nickname" class="required" style="margin-left: -1px;"><em>*</em><?php echo $this->__('Nickname') ?></label> 
<input type="text" name="nickname" id="nickname" value="<?php echo $this->htmlEscape($this->getCustomer()->getnickname()) ?>" title="<?php echo $this->__('Nickname') ?>" class="input-text required-entry" /> 

tôi cũng tự xóa các /var/cache thư mục nhưng không có bất kỳ tác dụng.

Tôi không biết mình có thể làm gì khác, vì vậy mọi trợ giúp đều rất được hoan nghênh.

EDIT:

Ok đây là những gì tôi đã cố gắng vào thư mục gốc Magento:

define('MAGENTO', realpath(dirname(__FILE__))); 
ini_set('memory_limit', '32M'); 
set_time_limit (0); 
require_once MAGENTO . '/app/Mage.php'; 

Mage::app(); 

$installer = $this; 
$installer->startSetup(); 

$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 

$entityTypeId  = $setup->getEntityTypeId('customer'); 
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId); 
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId); 

$setup->addAttribute('customer', 'nickname', array(
    'input'   => 'text', 
    'type'   => 'text', 
    'label'   => 'Nickname', 
    'visible'  => 1, 
    'required'  => 1, 
    'user_defined' => 1, 
)); 

$setup->addAttributeToGroup(
$entityTypeId, 
$attributeSetId, 
$attributeGroupId, 
'nickname', 
'999' //sort_order 
); 


$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'nickname'); 
$oAttribute->setData('used_in_forms', array('adminhtml_customer')); 

$oAttribute->save(); 
$installer->endSetup(); 

Mã này sẽ được thực hiện tốt và không có thông báo lỗi hoặc một cái gì đó nhưng cũng không có thuộc tính mới được thêm vào bảng eav_attribute.

+3

Theo thời gian, liên kết dán của bạn sẽ hết hạn và những người khác sẽ không thể sử dụng bài viết của bạn để giải quyết vấn đề của riêng họ. Vui lòng chỉnh sửa bài đăng của bạn và chỉ nội tuyến các phần có liên quan của mã trong bài đăng, như bạn đã làm với tệp thiết lập của mình. –

+0

@ joseph-mastey Bạn nói đúng, nhưng tôi đã cố gắng để thêm toàn bộ mã andit đã cutted và không còn đầy đủ, đó là lý do tại sao tôi sử dụng mysticpaste. Nhưng, tôi sẽ thử lại lần nữa. – tecmec

+0

@ joseph-mastey Tôi đã thêm Setup.php ở trên nhưng việc chuyển đổi mã xml không hoạt động đúng cách, nó sẽ bỏ qua nửa sau của thẻ. – tecmec

Trả lời

5

Tôi đã chỉnh sửa tập lệnh của bạn và nó hoạt động tốt cho tôi, tôi có thể thấy thuộc tính mới được sao lưu ngay bây giờ.

Thay thế này:

$installer = $this; 
$installer->startSetup(); 

$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 

Với điều này:

$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$setup->startSetup(); 
Các vấn đề liên quan