mage以程序方式更新选项下拉标签上的sort_order


magento programatically update sort_order on option dropdown labels

网上有关于如何更新属性选项标签的信息(例如。http://www.webspeaks.in/2012/05/addupdate-attribute-option-values.html)但是我如何只更新排序顺序?

我想这样做的原因是,我想按产品浏览次数对制造商进行排序。因此,最具全球视野的制造商被排在首位。

这真的很费力。您必须为每个商店创建一个标签阵列,这样在更新时就不会覆盖任何内容。然后可以将排序顺序附加到该数组。以下是使其工作的代码:

//build array of labels
        $attribute_model        = Mage::getModel('eav/entity_attribute');
        $attribute_code         = $attribute_model->getIdByCode('catalog_product', 'manufacturer');
        //get default label values for option
        $optionCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setAttributeFilter($attribute_code)
                ->setPositionOrder('desc', true)
                //->setStoreFilter($_eachStoreId)
                ->load();
        //build the data required to reset the labels to what they were (what a pain)       
        foreach ($optionCollection as $option) 
            $data['value'][$option->getData('option_id')][0] = $option->getData('value');

        //go through the stores one by one
        foreach (Mage::app()->getStores() as $_eachStoreId => $val) 
        {
            //get the labels for this attribute for that store
            $optionCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                    ->setAttributeFilter($attribute_code)
                    ->setPositionOrder('desc', true)
                    ->setStoreFilter($_eachStoreId)
                    ->load();
            //build the data required to reset the labels to what they were (what a pain)       
            foreach ($optionCollection as $option) 
                if( $data['value'][$option->getData('option_id')][0] != $option->getData('value') )
                    $data['value'][$option->getData('option_id')][$_eachStoreId] = $option->getData('value');
                else
                    $data['value'][$option->getData('option_id')][$_eachStoreId] = '';
        }
        //echo "<pre>"; print_r($data); die; 
        //just load all products with view counts and build manufacturerValueId => totalViews array
        $products = Mage::getResourceModel('reports/product_collection')
                    ->addViewsCount()
                    ->addAttributeToSelect('manufacturer')
                    ;
        foreach($products as $product){
            if ($product->getManufacturer()!='') $data['order'][$product->getManufacturer()] += $product->getViews();
        }
        //now we've gotta invert this array
        //put largest value at top
        arsort($data['order']);
        foreach($data['order'] as $key => $value)
        {
            if(!isset($highest)) $highest = $value; 
            $newData['order'][$key] = $highest - $value;
        }
        $data['order'] = $newData['order']; unset($newData);
        //echo "<pre>"; print_r($data); die;
        $data = array('option' => $data );
        //Get the eav attribute model
        $attr_model = Mage::getModel('catalog/resource_eav_attribute');
        //Load the particular attribute by id
        $attr_model->load($attribute_code);
        //Add data to our attribute model
        $attr_model->addData($data);
        //echo "<pre>"; print_r($attr_model->getData()); die; 
        //Save the updated model
        try {
            $attr_model->save();
            /**
             * Clear translation cache because attribute labels are stored in translation
             */
            $session = Mage::getSingleton('adminhtml/session');
            Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
            $session->setAttributeData(false);
            return;
        } catch (Exception $e) {
            echo "<pre>"; print_r($e->getMessage());
            echo "<pre>"; print_r($data);
            return;
        }

所以我意识到这是一个超级坏死,但如果你有可用的属性option_id,这里最简单的方法就是

$resource = Mage::getSingleton('core/resource');
$writeCxn = $resource->getConnection('core_write');
$query = "update 'eav_attribute_option' SET sort_order='".$sort_order."' WHERE 'option_id'='".$option_id."';";
$writeCxn->query($query);

把它放在你的数组上的foreach循环中,不要试图重写标签等等。这里的差距是,如果你试图在商店范围内应用排序订单。这将不起作用,但是,如果您只需要选项列表的一个排序顺序。这就是要走的路。