在注册表格magento中添加多个字段

时间:2013-12-12 16:36:53

标签: magento magento-1.7

我想在注册表单中添加多个字段。

安装文件是: -

<?php

     $installer = $this;
     $installer->startSetup();
     $setup = Mage::getModel('customer/entity_setup', 'core_setup');
     $setup->addAttribute('customer', 'accounttype', array(
'type' => 'int',
'input' => 'select',
'label' => 'Accounttype',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 1,
    'source' =>  'profile/entity_accounttype',
 ));

  $setup->addAttribute('customer', 'tva', array(
'type' => 'int',
'input' => 'select',
'label' => 'Tva',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 2,
'source' =>  'profile/entity_tva',
 ));
   $setup->addAttribute('customer', 'companycountry', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Companycountry',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 4,
'source' =>  'profile/entity_companycountry',
));
 $setup->addAttribute('customer', 'companycomment', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Companycomment',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 3,
'source' =>  'profile/entity_companycomment',
));
     if (version_compare(Mage::getVersion(), '1.6.0', '<='))
   {
$customer = Mage::getModel('customer/customer');
$attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
$setup->addAttributeToSet('customer', $attrSetId, 'General',            'accounttype','tva','companycountry','companycomment');
   }

  if (version_compare(Mage::getVersion(), '1.4.2', '>='))
{
Mage::getSingleton('eav/config')
->getAttribute('customer', 'accounttype',  'tva','companycountry','companycomment' )
->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
->save();

  }

 $tablequote = $this->getTable('sales/quote');
  $installer->run("
  ALTER TABLE  $tablequote ADD  `customer_accounttype` INT NOT NULL,
                      ADD `customer_tva` INT NOT NULL,
                ADD `customer_companycountry` varchar(100) NOT NULL,
                ADD `customer_companycomment` varchar(1000) NOT NULL
   ");

   $installer->endSetup();

我为每个字段制作了4个源模型。我们制作的只有一个领域&amp;数据也没有保存在数据库中。请帮我,我错了。

2 个答案:

答案 0 :(得分:0)

您可以浏览此链接Magento Module Creater

并使用Need Add Customer Attribute :创建模块是,它将为您的客户属性提供字段。并且创建字段和你一样多。在创建字段之后,只需单击Create Magento Module (download)它将为您提供一个包含自定义客户属性的就绪模块。 Note:- Selecte **Forms to Use In**,其中将填充您的字段以供用户输入

希望这会对你有所帮助。

答案 1 :(得分:0)

尝试此代码:

    $attributeArray = [
    [
        'code'      => 'is_privacy_statement',
        'label'     => 'Privacy Statement',
        'position'  => '1020'
    ],[
        'code'      => 'is_general_terms',
        'label'     => 'General Terms',
        'position'  => '1021'
    ]
];

foreach($attributeArray as $attributeCreate)
{
    $installer = new Mage_Customer_Model_Entity_Setup('core_setup');
    $installer->startSetup();

    $installer->addAttribute(
        "customer", $attributeCreate['code'], array(
            "type"      => "int",
            "label"     => $attributeCreate['label'],
            "input"     => "text",
            "visible"   => true,
            "required"  => true,
            "default"   => "1",
        )
    );

    $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", $attributeCreate['code']);

    $used_in_forms = array();

    $used_in_forms[] = "adminhtml_customer";
    $used_in_forms[] = "checkout_register";
    $used_in_forms[] = "customer_account_create";
    $used_in_forms[] = "customer_account_edit";

    $attribute->setData("used_in_forms", $used_in_forms)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", $attributeCreate['position']);

    $attribute->save();
    $installer->endSetup();
}

注意:输入类型已根据“ {On customer register got an error message: “Cannot save the customer””进行了更改,因为复选框和/或布尔类型出现错误,并且代码进行了一些优化。