制作一个模块,创建一些客户EAV属性。
其中一个属性是选择,我将一堆选项放入各自的表中。
所有东西都排成一列,前后两个都可以看到。
调用这部分内容之前的最后一件事是选项的排序顺序。
他们全都出来了,而不是明显的默认或字母(看似随意......非常奇怪)。
我在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安装-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();
应用/代码/本地/ WACI / CustomerAttr /型号/客户/属性/数据/ 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;
}
}
主题/变更/模板/持久性/客户/形式/ 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>
所有选项都已加载到表eav_attribute_option
中(尽管没有定义sort_order
),以及表eav_attribute_option_value
。
在 adminhtml / customer-&gt; manage customers-&gt;帐户信息中,此选项显示正常(但由系统自动提供)。
似乎我应该能够在创建attributeOptions时设置排序顺序,或者当然,在data/select
类中定义排序顺序。但我没有尝试过任何工作。
我宁愿不做前端黑客......
哦,如何设置此选择的默认值? (不同的问题,我知道,但相关)。设置属性'default'=&gt; '华盛顿'似乎什么都不做。
似乎有很多方法可以像这样设置属性选择选项。我在这里概述了一个更好的方法吗?也许我搞砸了什么。
干杯
答案 0 :(得分:0)
......好吧,这太荒谬了。
<div class="input-box">
<select name="state" id="state" class="<?php if($attribute->getIsRequired() == true){?>required-entry<?php } ?>">
<?php
$options = $attribute->getSource()->getAllOptions();
sort($options);
foreach($options as $option){
?>
<option value='<?php echo $option['value']?>' <?php if($option['label'] == 'Washington'){ echo 'selected="selected"';}?>><?php echo $this->__($option['label'])?></option>
<?php } ?>
</select>
</div>
......只是假设事情比他们更复杂。
我把它归结为“我的大脑很糊涂”