Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - create new customer attribute

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?

like image 227
odd_duck Avatar asked Dec 20 '22 09:12

odd_duck


1 Answers

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

 mysql4-upgrade-0.0.2-0.0.1.php

So now in this you can add your installer script like,

$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 you need to update the config.xml file also. So it should be,

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

For more information, go here. If you have any doubt please comment here.

Update: I'm sorry. The file name should be like this,

   mysql4-upgrade-0.0.2-0.0.1.php

The core_resource table contains module entries. Now the 0.0.2 is also updated. So magento will not look up your module(updated) xml files to load. so you need to again change the file name to mysql4-upgrade-0.0.3-0.0.2.php or delete that entry in your database and rename your module version to fresh like mysql4-upgrade-0.0.0.php

Update-2:

Here I have attached new codes, I checked in my local its working fine,

app/code/local/Packagename/Modulename/etc/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> 

app/code/local/Packagename/Modulename/Helper/Data.php

<?php
class Packagename_Modulename_Helper_Data extends Mage_Core_Helper_Abstract
{
}

app/code/local/Packagename/Modulename/sql/customerattribute1415104755_setup/mysql4-install-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();

And finally enable your module in, app/etc/modules/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>
like image 144
Elavarasan Avatar answered Jan 06 '23 16:01

Elavarasan