如何在以编程方式添加属性时设置排序顺序

How to set sort order while add attribute programmatically

我想在属性中设置排序顺序。在 mysql 文件中我使用了这个

$installer->addAttribute('customer','badge', array( 
'label'             => 'Badge',
'type'              => 'text',    //backend_type
'input'             => 'multiselect', //frontend_input
'backend'           => 'eav/entity_attribute_backend_array',    
'global'            =>  Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source'            => 'marketplace/eav_entity_attribute_source_badge', // Goes to Step 2
'visible'           => true,
'required'          => false,
'default'           => '',
'frontend'          => '',
'unique'            => false,
'note'              => '',
'sort_order'        => 10

 ));
Mage::getSingleton('eav/config')
->getAttribute('customer', 'badge')
->setData('used_in_forms', array('customer_account_create','customer_account_edit','customer_address_edit','checkout_onepage_register','checkout_onepage_register_guest','checkout_onepage_billing_address','adminhtml_customer','checkout_onepage_shipping_address','checkout_multishipping_register'))
->save();

但是如何设置此属性的排序顺序不起作用

当您以编程方式添加属性时,您必须像这样设置顺序

 Mage::getSingleton('eav/config')
  ->getAttribute('customer', 'badge')
  ->setSortOrder(100)
   ->setData('used_in_forms', array('customer_account_create','customer_account_edit','customer_address_edit','checkout_onepage_register','checkout_onepage_register_guest','checkout_onepage_billing_address','adminhtml_customer','checkout_onepage_shipping_address','checkout_multishipping_register'))
   ->save();

'sort_order' 无效

您必须使用位置而不是 sort_order。

'position' => 20

来自Mage_Customer_Model_Resource_Setup

/**
 * Prepare customer attribute values to save in additional table
 *
 * @param array $attr
 * @return array
 */
protected function _prepareValues($attr)
{
    $data = parent::_prepareValues($attr);
    $data = array_merge($data, array(
        'is_visible'                => $this->_getValue($attr, 'visible', 1),
        'is_system'                 => $this->_getValue($attr, 'system', 1),
        'input_filter'              => $this->_getValue($attr, 'input_filter', null),
        'multiline_count'           => $this->_getValue($attr, 'multiline_count', 0),
        'validate_rules'            => $this->_getValue($attr, 'validate_rules', null),
        'data_model'                => $this->_getValue($attr, 'data', null),
        'sort_order'                => $this->_getValue($attr, 'position', 0)
    ));

    return $data;
}

您可以使用以下代码在 app/local/NAMESPACE/NAME/sql/NAME_setup 文件夹中创建一个新的 upgrade.php

<?php
// This installer scripts update a product attribute to Magento programmatically.

$model = Mage::getModel('catalog/resource_eav_attribute');
$model->loadByCode(4, 'color');

// Create attribute:
// We create a new installer class here so we can also use this snippet in a non-EAV setup script.
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();

if ($model) {
    $model->setPosition(3)
        ->save();
}
// Done:
$installer->endSetup();

并且不要忘记在 app/local/NAMESPACE/NAME/etc/ 中更新 config.xml 包含以下代码的文件夹:

<config>
    <modules>
        <Namespace_Name>
            <version>0.0.2</version>
        </Namespace_Name>
    </modules>
    <global>
        <resources>
            <name_setup>
                <setup>
                    <module>Namespace_Name</module>
                </setup>
            </name_setup>
        </resources>
    </global>
</config>