module - Magento - create new customer attribute -
i need create 4 new client attributes in magento store. have created module below 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 { /** * method returns true if attribute exists. * * @param string|int $entitytypeid * @param string|int $attributeid * @return bool */ public function attributeexists($entitytypeid, $attributeid) { seek { $entitytypeid = $this->getentitytypeid($entitytypeid); $attributeid = $this->getattributeid($entitytypeid, $attributeid); homecoming !empty($attributeid); } catch(exception $e) { homecoming 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 first client attribute of paypal want able add together 3 others. had hoped if alter mysql4-install-0.0.1.php file 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 new file , went onto site attribute_2 added isn't.
why work 1 time not again?
you need add together new file(installer script) , file name should be,
mysql4-upgrade-0.0.2-0.0.1.php so in can add together 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 need update config.xml file also. 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 have uncertainty please comment here.
update: i'm sorry. file name should this,
mysql4-upgrade-0.0.2-0.0.1.php the core_resource table contains module entries. 0.0.2 updated. magento not module(updated) xml files load. need 1 time again alter file name mysql4-upgrade-0.0.3-0.0.2.php or delete entry in database , rename module version fresh mysql4-upgrade-0.0.0.php
update-2:
here have attached new codes, checked in local 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 enable 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> magento module attributes
No comments:
Post a Comment