Magento – Magento 1.9.2 Sort attribute by position

attributesmagento-1.9sort

I have the following code but need to sort by the position value set for each attribute option in the back end.

        $this->_optionCollection = Mage::getResourceModel('attributepages/entity_collection')
            ->addOptionOnlyFilter()
            ->addFieldToFilter('attribute_id', $parentPage->getAttributeId())
            ->addUseForAttributePageFilter()
            ->addStoreFilter($storeId)
            ->setOrder('main_table.title', 'asc');  

I have tried the following:

->setOrder('main_table.position', 'asc');  
->setOrder('main_table.sort_order', 'asc');  
->setOrder('position', 'asc');  
->setOrder('sort_order', 'asc');  

All Fail. Can anyone assist?

FULL CODE

protected function _getOptionCollection()
{
    if (null === $this->_optionCollection && $this->getCurrentPage()) {
        $storeId = Mage::app()->getStore()->getId();
        $parentPage = $this->getCurrentPage();
        $this->_optionCollection = Mage::getResourceModel('attributepages/entity_collection')
            ->addOptionOnlyFilter()
            ->addFieldToFilter('attribute_id', $parentPage->getAttributeId())
            ->addUseForAttributePageFilter()
            ->addStoreFilter($storeId)
            ->setOrder('main_table.title', 'asc');

        if ($excludedOptions = $parentPage->getExcludedOptionIdsArray()) {
            $this->_optionCollection
                ->addFieldToFilter('option_id', array(
                    'nin' => $excludedOptions
                ));
        }

        if ($limit = $this->getLimit()) {
            $this->_optionCollection->setPageSize($limit);
        }

        // filter options with the same urls: linked to All Store Views and current store
        $urls = $this->_optionCollection->getColumnValues('identifier');
        $duplicateUrls = array();
        foreach (array_count_values($urls) as $url => $count) {
            if ($count > 1) {
                $duplicateUrls[] = $url;
            }
        }
        foreach ($duplicateUrls as $url) {
            $idsToRemove = array();
            $removeFlag = false;
            $options = $this->_optionCollection->getItemsByColumnValue('identifier', $url);
            foreach ($options as $option) {
                if ($option->getStoreId() !== $storeId) {
                    $idsToRemove[] = $option->getId();
                } else {
                    $removeFlag = true;
                }
            }
            if ($removeFlag) {
                foreach ($idsToRemove as $id) {
                    $this->_optionCollection->removeItemByKey($id);
                }
            }
        }

        foreach ($this->_optionCollection as $option) {
            $option->setParentPage($parentPage);
        }
    }
    return $this->_optionCollection;
}

Best Answer

I dont know but you can try below code and check

        $this->_optionCollection = Mage::getModel('attributepages/entity')->getCollection()
        ->addOptionOnlyFilter()
        ->addFieldToFilter('attribute_id', $parentPage->getAttributeId())
        ->addUseForAttributePageFilter()
        ->addStoreFilter($storeId)
        ->setOrder('position', 'asc');

If you need attribute options with sort you can try below code

$options = Mage::getResourceModel('eav/entity_attribute_option_collection')
        ->setAttributeFilter($attributeId)
        ->setPositionOrder('desc', true)
        ->load();

Try and let me know its working or not

Related Topic