Customer Attribute, not sorting select options
- by Bosworth99
Made a module that creates some customer EAV attributes.
One of these attributes is a Select, and I'm dropping a bunch of options into their respective tables.
Everything lines up and is accessible on both the front end and the back.
Last thing before calling this part of things finished is the sort order of the options.
They come out all scrambled, instead of the obvious default or alphabetical (seemingly at random... very wierd).
I'm on Mage v1.11 (Pro/Enterprise).
config.xml
<config>
<modules>
<WACI_CustomerAttr>
<version>0.1.0</version>
</WACI_CustomerAttr>
</modules>
<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>
<models>
<WACI_CustomerAttr>
<class>WACI_CustomerAttr_Model</class>
</WACI_CustomerAttr>
</models>
<fieldsets>
<customer_account>
<agency><create>1</create><update>1</update></agency>
<title><create>1</create><update>1</update></title>
<phone><create>1</create><update>1</update></phone>
<mailing_address><create>1</create><update>1</update></mailing_address>
<city><create>1</create><update>1</update></city>
<state><create>1</create><update>1</update></state>
<zip><create>1</create><update>1</update></zip>
<fed_id><create>1</create><update>1</update></fed_id>
<ubi><create>1</create><update>1</update></ubi>
</customer_account>
</fieldsets>
</global>
</config>
mysql4-install-0.1.0.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();
// bunch of attributes
// State
$installer->addAttribute('customer','state',
array(
'type' => 'varchar',
'group' => 'Default',
'label' => 'State',
'input' => 'select',
'default' => 'Washington',
'source' => 'WACI_CustomerAttr/customer_attribute_data_select',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'required' => true,
'visible' => true,
'user_defined' => 1,
'position' => 67
)
);
$attrS = Mage::getSingleton('eav/config')->getAttribute('customer', 'state');
$attrS->addData(array('sort_order'=>67));
$attrS->setData('used_in_forms', array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
$state_list = array('Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','Florida','Georgia',
'Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan',
'Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York',
'North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota',
'Tennessee','Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming');
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId('customer', 'state');
for($iCount=0;$iCount<sizeof($state_list);$iCount++){
$aOption['value']['option'.$iCount][0] = $state_list[$iCount];
}
$installer->addAttributeOption($aOption);
// a few more
$installer->endSetup();
app/code/local/WACI/CustomerAttr/Model/Customer/Attribute/Data/Select.php
<?php
class WACI_CustomerAttr_Model_Customer_Attribute_Data_Select extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
function getAllOptions(){
if (is_null($this->_options)) {
$this->_options = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($this->getAttribute()->getId())
->setStoreFilter($this->getAttribute()->getStoreId())
->setPositionOrder('asc')
->load()
->toOptionArray();
}
$options = $this->_options;
return $options;
}
}
theme/variation/template/persistent/customer/form/register.phtml
<li>
<?php
$attribute = Mage::getModel('eav/config')->getAttribute('customer','state');
?>
<label for="state" class="<?php if($attribute->getIsRequired() == true){?>required<?php } ?>"><?php if($attribute->getIsRequired() == true){?><em>*</em><?php } ?><?php echo $this->__('State') ?></label>
<div class="input-box">
<select name="state" id="state" class="<?php if($attribute->getIsRequired() == true){?>required-entry<?php } ?>">
<?php
$options = $attribute->getSource()->getAllOptions();
foreach($options as $option){
?>
<option value='<?php echo $option['value']?>' <?php if($this->getFormData()->getState() == $option['value']){ echo 'selected="selected"';}?>><?php echo $this->__($option['label'])?></option>
<?php } ?>
</select>
</div>
</li>
All options are getting loaded into table eav_attribute_option just fine (albeit without a sort_order defined), as well as table eav_attribute_option_value.
In the adminhtml / customer-manage customers-account information this select is showing up fine (but its delivered automatically by the system).
Seems I should be able to set the sort-order on creation of the attributeOptions, or, certainly, define the sort order in the data/select class. But nothing I've tried works.
I'd rather not do a front-end hack either...
Oh, and how do I set the default value of this select? (Different question, I know, but related). Setting the attributes 'default' = 'washington' seems to do nothing.
There seem to be a lot of ways to set up attribute select options like this. Is there a better way that the one I've outlined here? Perhaps I'm messing something up.
Cheers