0

制作了一个创建一些客户 EAV 属性的模块。其中一个属性是Select,我将一堆选项放入它们各自的表中。一切都排成一行,并且在前端和后端都可以访问。

在调用这部分事情完成之前的最后一件事是选项的排序顺序。它们都是乱码的,而不是明显的默认值或字母顺序(似乎是随机的……非常奇怪)。

我正在使用 Mage v1.11(专业版/企业版)。

配置文件

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

    }
}

主题/变体/模板/持久性/客户/表单/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很好地加载到 table 中(尽管没有sort_order定义),以及 table eav_attribute_option_value

adminhtml / customer->manage customers->account information中,这个选择显示得很好(但它是由系统自动提供的)。

似乎我应该能够在创建属性选项时设置排序顺序,或者,当然,在data/select类中定义排序顺序。但是我尝试过的没有任何效果。我也不想做前端黑客......

哦,我如何设置这个选择的默认值?(不同的问题,我知道,但相关)。设置属性 'default' => 'washington' 似乎无济于事。

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

干杯

4

1 回答 1

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>

...只是假设事情比他们更复杂。

我把它归结为“我的大脑是糊状的”

于 2012-09-05T15:25:27.983 回答