创建了一个模块,创建一些客户EAV属性。 其中一个属性是选择,我将一堆选项放到它们各自的表中。全部排队并且在前端和后端均可访问。
在调用这部分内容之前的最后一件事是选项的排序顺序。 他们出来所有的炒作,而不是明显的默认或字母(看似随意......非常奇怪)。客户属性,不排序选择选项
我在法师v1.11(专业/企业)。
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 /型号/客户/属性/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;
}
}
主题/变更/模板/永久/客户/表格/ 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-> manage customers->帐户信息此选择显示正常(但由系统自动发送)。
似乎我应该能够在创建attributeOptions时设置排序顺序,或者当然也可以在data/select
类中定义排序顺序。但我没有尝试过。 我宁愿不做前端破解...
哦,我该如何设置此选择的默认值? (不同的问题,我知道,但相关)。设置属性'默认'=>'华盛顿'似乎什么都不做。
似乎有很多方法来设置这样的属性选择选项。有没有更好的方法,我在这里概述的那个?也许我在搞点东西。
干杯
为什么编辑?我想在标题中使用“magento”。没有参考软件,线程就没有意义。无论如何,或者容易混淆。这是一个Stackoverflow标准吗? – Bosworth99
基于这个原因http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles – j08691
嗯,这是一个很好的概念......在纸上。然而,每个人都这样做,因为它很有意义(只是看看SO问题= 50%在标题中使用标签) - 为什么改变人们明确想要使用系统? – Bosworth99