1

I need to create 4 new customer attributes in my magento store. I have created a module as below to do so:

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 customer attribute of paypal but i now want to be able to add 3 others. I had hoped that if i change the mysql4-install-0.0.1.php file to 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 the new file and went back onto the site attribute_2 would be added but it isn't.

Why does this work once but not again?

1

Install files are only called once. You will have to add your code to a file called mysql4-upgrade-0.0.1-0.0.2 and change the version in your confix.xml to 0.0.2.

Also, what Magento Version are you using? Calling the setup class Custom_Customerattribute_Model_Mysql4_Setup and prefixing the install and upgrade files with mysql4 is deprecated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.