簡體   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