繁体   English   中英

magento按位置对属性选项进行排序?

[英]magento sort attribute option collection by position?

问候,

我正在尝试按在“管理属性”面板中输入的属性选项值的“位置”对属性数组进行排序。 我似乎已经尝试了所有方法,有人知道这怎么可能吗?

我以为这肯定行得通:

    $_collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
        ->setStoreFilter(0)
        ->setAttributeFilter($_productAttribute->getId())
        ->addAttributeToSort('position')
        ->load();

但事实并非如此。 任何帮助将不胜感激!

我在以前的项目中对addAttributeToSort经验丰富:也许直到今天尝试使用setOrder('columname')或尝试将您的magento更新为最新版本,该功能才有效

$attribute = Mage::getModel('eav/entity_attribute')->load( $code, 'attribute_code');
$option_col = Mage::getResourceModel( 'eav/entity_attribute_option_collection')
 ->setAttributeFilter( $attribute->getId() )
 ->setStoreFilter()
 ->setPositionOrder( 'ASC' );
$option_col->getSelect()->order('main_table.sort_order '.$orderby);

在app / design / frontend / default / default / template / manapro / filtercheckboxes / items.phtml的开头添加以下代码:

function cmp($a, $b){
  if ($a == $b)
    return 0;
  return ($a['position'] < $b['position']) ? -1 : 1;
}
$array = $this->getItems();
usort($array, "cmp");

并在foreach循环中将$ this-> getItems()替换为$ array。

在进行eav收集时,将加载联接查询加载到收集中并使用加载功能。 因此,如果您在之后添加订单

Mage::getResourceModel('eav/entity_attribute_option_collection')

就像:

$_collection = Mage::getResourceModel('eav/entity_attribute_option_collection')->getSelect()->order('main_table.sort_order '.$orderby);
        $_collection->setStoreFilter(0)
        ->setAttributeFilter($_productAttribute->getId())
        ->load();

工作很棒。 在Magento 1.6及setOrder('sort_order')使用setOrder('sort_order')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM