Magento - create a new customer attribute

I need to create 4 new customer attributes in my magento store. To do this, I created a module as shown below:

Customerattribute >
    etc > config.xml
    Model > Mysql4 > Setup.php
    sql > customerattribute_setup > mysql4-install-0.0.1.php

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Custom_Customerattribute>
      <version>0.0.1</version>
    </Custom_Customerattribute>
  </modules>
    <global>
        <resources>
            <customerattribute_setup>
                <setup>
                    <module>Custom_Customerattribute</module>
                    <class>Custom_Customerattribute_Model_Mysql4_Setup</class>
                </setup>
                ....
            </customerattribute_setup>
        </resources>
    </global>
</config>

setup.php

class Custom_Customerattribute_Model_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup 
{
    /**
     * This method returns true if the attribute exists.
     * 
     * @param string|int $entityTypeId
     * @param string|int $attributeId
     * @return bool
     */
    public function attributeExists($entityTypeId, $attributeId) 
    {
        try 
        {
            $entityTypeId = $this->getEntityTypeId($entityTypeId);
            $attributeId = $this->getAttributeId($entityTypeId, $attributeId);
            return !empty($attributeId);
        } 
        catch(Exception $e) 
        {
            return FALSE;
        }
    }
}

mysql4-install-0.0.1.php

$installer = $this;
$installer->startSetup();
$entity = $installer->getEntityTypeId('customer');

if(!$installer->attributeExists($entity, 'paypal')) {
    $installer->removeAttribute($entity, 'paypal');
}

$installer->addAttribute($entity, 'paypal', array(
        'type' => 'text',
        'label' => 'Paypal',
        'input' => 'text',
        'visible' => TRUE,
        'required' => FALSE,
        'default_value' => '',
        'adminhtml_only' => '0'
));

$forms = array(
    'adminhtml_customer',
    'customer_account_edit'
);
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'paypal');
$attribute->setData('used_in_forms', $forms);
$attribute->save();

$installer->endSetup();

This works for my first client attribute paypal, but now I want to be able to add 3 others. I was hoping that if I modify the file mysql4-install-0.0.1.phpto say this:

$installer = $this;
$installer->startSetup();
$entity = $installer->getEntityTypeId('customer');

if(!$installer->attributeExists($entity, 'attribute_2')) {
    $installer->removeAttribute($entity, 'attribute_2');
}

$installer->addAttribute($entity, 'attribute_2', array(
        'type' => 'text',
        'label' => 'Attribute 2',
        'input' => 'text',
        'visible' => TRUE,
        'required' => FALSE,
        'default_value' => '',
        'adminhtml_only' => '0'
));

$forms = array(
    'adminhtml_customer',
    'customer_account_edit'
);
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'attribute_2');
$attribute->setData('used_in_forms', $forms);
$attribute->save();

$installer->endSetup();

and uploaded a new file and returned to the site attribute_2, but it’s not.

Why does it work once, but not again?

+4
source share
2 answers

You need to add a new file (installer script) and the file name should be

 mysql4-upgrade-0.0.2-0.0.1.php

So now you can add your installer script, for example,

$installer = $this;
$installer->startSetup();
$entity = $installer->getEntityTypeId('customer');

if(!$installer->attributeExists($entity, 'attribute_2')) {
    $installer->removeAttribute($entity, 'attribute_2');
}

$installer->addAttribute($entity, 'attribute_2', array(
        'type' => 'text',
        'label' => 'Attribute 2',
        'input' => 'text',
        'visible' => TRUE,
        'required' => FALSE,
        'default_value' => '',
        'adminhtml_only' => '0'
));

$forms = array(
    'adminhtml_customer',
    'customer_account_edit'
);
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'attribute_2');
$attribute->setData('used_in_forms', $forms);
$attribute->save();

$installer->endSetup();

config.xml. ,

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Custom_Customerattribute>
      <version>0.0.2</version>
    </Custom_Customerattribute>
  </modules>
    <global>
        <resources>
            <customerattribute_setup>
                <setup>
                    <module>Custom_Customerattribute</module>
                    <class>Custom_Customerattribute_Model_Mysql4_Setup</class>
                </setup>
                ....
            </customerattribute_setup>
        </resources>
    </global>
</config>

. , .

Update: . ,

   mysql4-upgrade-0.0.2-0.0.1.php

core_resource . 0.0.2. magento () xml . mysql4-upgrade-0.0.3-0.0.2.php , mysql4-upgrade-0.0.0.php

Update-2:

, ,

//​​//MODULENAME/ .. /config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Packagename_Modulename>
      <version>0.0.0</version>
    </Packagename_Modulename>
  </modules>
  <global>
    <helpers>
      <modulename>
        <class>Packagename_Modulename_Helper</class>
      </modulename>
    </helpers>
    <models>
      <modulename>
        <class>Packagename_Modulename_Model</class>
        <resourceModel>modulename_mysql4</resourceModel>
      </modulename>
    </models>
    <resources>
      <customerattribute1415104755_setup>
        <setup>
          <module>Packagename_Modulename</module>
          <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </customerattribute1415104755_setup>
      <customerattribute1415104755_write>
        <connection>
          <use>core_write</use>
        </connection>
      </customerattribute1415104755_write>
      <customerattribute1415104755_read>
        <connection>
          <use>core_read</use>
        </connection>
      </customerattribute1415104755_read>
    </resources>
  </global>
</config> 

//​​//MODULENAME/Helper/Data.php

<?php
class Packagename_Modulename_Helper_Data extends Mage_Core_Helper_Abstract
{
}

//​​//MODULENAME/SQL/customerattribute1415104755_setup/mysql4--0.0.0.php

<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "myattrbute1",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "My attribute-1",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute1");


$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";
$used_in_forms[]="adminhtml_checkout";
        $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", 100)
        ;
        $attribute->save();




$installer->addAttribute("customer", "myattrbute2",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "My attribute-2",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute2");


$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";
$used_in_forms[]="adminhtml_checkout";
        $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", 100)
        ;
        $attribute->save();




$installer->addAttribute("customer", "myattrbute3",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "My attribute-3",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute3");


$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";
$used_in_forms[]="adminhtml_checkout";
        $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", 100)
        ;
        $attribute->save();




$installer->addAttribute("customer", "myattrbute4",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "My attribute-4",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute4");


$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";
$used_in_forms[]="adminhtml_checkout";
        $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", 100)
        ;
        $attribute->save();



$installer->endSetup();

, , , / ..//Packagename_Modulename.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Packagename_Modulename>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.0.0</version>
    </Packagename_Modulename>
  </modules>
</config>
+10

, , :

if(!$installer->attributeExists($entity, 'attribute_2')) {
    $installer->removeAttribute($entity, 'attribute_2');
}

... , " - "

, :

if ($installer->attributeExists($entity, 'attribute_2')) {
    $installer->removeAttribute($entity, 'attribute_2');
}
0

All Articles