Been banging my head on this for 8 hours now...
I have a need (or rather, my company does, being a B2B) to request extra attributes at customer registration. Of course Magento doesn't do this natively, so we're forced down wildly convoluted build path to make it so. I bought a module to do this, but found the code to be a hot mess (not to mention simply not working).
Whatever. I guess I'll just roll my own.
So I found a tutorial that seemed clear and to the point. So I, spends a few minutes putting it together. And.... nothing.
Further google-fu leads me here and here for that extra hot wisdom sauce.
Here's what I've got (running Magento Professional v1.11 ):
File structure:
WACI
- Customer
-- etc
--- config.xml
-- Model
--- Resource
---- Eav
----- Mysql4
------ Setup.php
-- sql
--- customer_setup
---- mysql4-install-0.1.0.php
etc/modules/WACI_All.xml
<config>
<modules>
<WACI_Customer>
<active>true</active>
<codePool>local</codePool>
</WACI_Customer>
</modules>
</config>
config.xml
<config>
<modules>
<WACI_Customer>
<version>0.1.0</version>
</WACI_Customer>
</modules>
<global>
<fieldsets>
<customer_account>
<title><create>1</create><update>1</update></title>
<phone><create>1</create><update>1</update></phone>
<agency><create>1</create><update>1</update></agency>
<fed_id><create>1</create><update>1</update></fed_id>
<ubi><create>1</create><update>1</update></ubi>
</customer_account>
</fieldsets>
<!--<models>
<customer>
<class>WACI_Customer_Model</class>
</customer>
</models> -->
<resources>
<customer_setup>
<setup>
<module>WACI_Customer</module>
<class>WACI_Customer_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customer_setup>
<customer_write>
<connection>
<use>core_write</use>
</connection>
</customer_write>
<customer_read>
<connection>
<use>core_read</use>
</connection>
</customer_read>
</resources>
</global>
</config>
Setup.php
<?php
class WACI_Customer_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
public function getDefaultEntities()
{
return array(
'customer' => array(
'entity_model' => 'customer/customer',
'table' => 'customer/entity',
'increment_model' => 'eav/entity_increment_numeric',
'increment_per_store' => false,
'attribute_model' => 'customer/attribute',
'attributes' => array(
'title' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Title / Position',
'visible' => true,
'required' => false,
'position' => 63,
),
'phone' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Telephone',
'visible' => true,
'required' => true,
'position' => 64,
),
'agency' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Agency / Organization',
'visible' => true,
'required' => false,
'position' => 65,
),
'fed_id' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Fed ID',
'visible' => true,
'required' => false,
'position' => 66,
),
'ubi' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'UBI',
'visible' => true,
'required' => false,
'position' => 67,
),
),
),
);
}
}
?>
mysql4-install-0.1.0.php
<?php
Mage::log('Installing WACI_Customer');
// die ( echo 'Running This Upgrade: '.get_class($this)."\n <br /> \n"; );
$installer = $this;
$installer->installEntities();
$eavConfig = Mage::getSingleton(‘eav/config’);
$attribute_title = $eavConfig->getAttribute(‘customer’, 'title');
$attribute_phone = $eavConfig->getAttribute(‘customer’, 'phone');
$attribute_agency = $eavConfig->getAttribute(‘customer’, 'agency');
$attribute_fedid = $eavConfig->getAttribute(‘customer’, 'fed_id');
$attribute_ubi = $eavConfig->getAttribute(‘customer’, 'ubi');
// put into customer_form_attribute table so field will show in admin.
$attribute_title->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_phone->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_agency->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_fedid->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_ubi->setData(‘used_in_forms’, array(‘adminhtml_customer’));
?>
Assuming I understand it correctly, I have everything in place... but nothing, nothing and more nothing.
notably:
core_config_data
core_resource
Question, then is two part:
1. What is preventing my install script from running?
2. Is the logic for creating new customer attributes sound?
Assuming it is - i think getting it onto the registration page / customer account / admin account should be fairly straightforward...
... Gonna go drink beer now.
Cheers.
as @AlexeiYerofeyev thought, the customer
name itself was the issue. Changed to CustomerAttr, and the script ran immediately.
The module then functioned about as expected, but @benmarks's solution seems cleaner, so I rewrote to match:
config.xml 0.1.0
<global>
<resources>
<customerattr_setup>
<setup>
<module>WACI_CustomerAttr</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customerattr_setup>
</resources>
<fieldsets>
<customer_account>
<title><create>1</create><update>1</update></title>
<phone><create>1</create><update>1</update></phone>
<agency><create>1</create><update>1</update></agency>
<fed_id><create>1</create><update>1</update></fed_id>
<ubi><create>1</create><update>1</update></ubi>
</customer_account>
</fieldsets>
</global>
</config>
setup.php
<?php
Mage::log('Installing WACI_CustomerAttr');
echo 'Running Upgrade: '.get_class($this)."\n <br /> \n";
//die ( 'its running' );
$installer = $this;
/* @var $installer Mage_Customer_Model_Entity_Setup */
$installer->startSetup();
$installer->addAttribute('customer','agency',
array(
'type' => 'varchar',
'label' => 'Agency / Organization',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 62,
)
);
$installer->addAttribute('customer','title',
array(
'type' => 'varchar',
'label' => 'Title / Position',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 63,
)
);
$installer->addAttribute('customer','phone',
array(
'type' => 'varchar',
'label' => 'Telephone',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 64,
)
);
$installer->addAttribute('customer','fed_id',
array(
'type' => 'varchar',
'label' => 'Fed ID',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 65,
)
);
$installer->addAttribute('customer','ubi',
array(
'type' => 'varchar',
'label' => 'UBI',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 66,
)
);
$attrs = array('agency','title','phone','fed_id','ubi');
foreach ($attrs as $item) {
$attr = Mage::getSingleton('eav/config')->getAttribute('customer', $item);
$attr->setIsUsedInForms(array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
}
$installer->endSetup();
?>
The module (and version) gets successfully written to core_resource
, and attributes are successfully getting added to eav_attribute
.
I can call the field up in
theme/template/customer/form/edit.phtml
<div class="input-box">
<label for="agency"><?php echo $this->__('Agency / Organization') ?><span class="required">*</span></label><br />
<input type="text" name="agency" id="agency" value="<?php echo $this->htmlEscape($this->getCustomer()->getAgency()) ?>" title="<?php echo $this->__('Agency') ?>" class="required-entry input-text" />
</div>
But, unfortunately, the value is not getting written to customer_entity_varchar
(which was, ultimately, what my initial script did as well.
So, I've got the attribute in the table, but its not getting added to the customer entity as of yet.
Seems like once I have that working, I should be able to read and write that value at will, wherever required.
Any thoughts on how I might proceed at this point?
$attr->setData('used_in_forms', array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
instead of
$attr->setIsUsedInForms(array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
For anyone who may need the info...
Turns out the array was not getting updated in the customer_form_attribute
table with the prior code.
There is a chance that your install script doesn't run because of its setup resource name (customer_setup). Since core extension Mage_Customer has the same name of setup resource and it is already installed in core_resource table with version 1.6.2.0 or something like that, your install script with version 0.1.0 may be considered as too old and is being ignored. So you can try renaming your resource to something unique.