客户属性,而不是排序选择选项


Customer Attribute, not sorting select options

制作了一个模块,用于创建一些客户EAV属性。其中一个属性是Select,我将把一堆选项放入它们各自的表中。所有东西都排成一排,前端和后端都可以访问

在调用这部分内容完成之前的最后一件事是选项的排序顺序。它们都是打乱的,而不是明显的默认值或字母顺序(看起来是随机的……非常模糊)。

我在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;
    }
}

主题/变体/模板/持久性/客户/表单/注册.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-customs->帐户信息中,此选择显示良好(但由系统自动提供)。

似乎我应该能够在创建attributeOptions时设置排序顺序,或者,当然,在data/select类中定义排序顺序。但我尝试过的都不管用。我也不想做前端破解

哦,我该如何设置这个选择的默认值?(不同的问题,我知道,但相关)。设置属性"default"=>"washington"似乎没有任何作用。

似乎有很多方法可以设置这样的属性选择选项。有比我在这里概述的更好的方法吗?也许我搞砸了什么。

干杯

。。。这太荒谬了。

<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>

只是假设事情比实际情况更复杂。

我把它归结为"我的大脑一团糟"