网上有关于如何更新属性选项标签的信息(例如http://www.webspeaks.in/2012/05/addupdate-attribute-option-values.html)但我如何只更新排序顺序?
我想这样做的原因是我想按产品查看次数对制造商进行排序。所以全球视野最多的厂商被排在最前面。
网上有关于如何更新属性选项标签的信息(例如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);
把它扔到你的resorted数组的foreach循环中,不要试图重写标签等等。这里的差距是如果您尝试在商店范围内应用排序顺序。这对此不起作用,但是,如果您只需要选项列表的一种排序顺序。这是要走的路。