2

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;
}
2
  • Please provide full code. Sep 5, 2016 at 11:17
  • Added full code to main question.
    – hejhog
    Sep 5, 2016 at 11:33

1 Answer 1

0

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

6
  • This produces the top header only and then a blank output.
    – hejhog
    Sep 5, 2016 at 11:49
  • Check again updated answer Sep 5, 2016 at 11:52
  • This threw an exception. From the error message I can see the code above (which is from a third party module) pulls the attributes from it's own sql. SELECT main_table.*, store_table.store_id FROM mg_tm_attributepages_entity AS main_table INNER JOIN mg_tm_attributepages_entity_store AS store_table ON main_table.entity_id = store_table.entity_id WHERE (attribute_id = '241') AND (option_id IS NOT NULL) AND (use_for_attribute_page = '1') AND (store_table.store_id IN('1', 0)) GROUP BY main_table.entity_id ORDER BY position ASC" Is it possible to form a JOIN?
    – hejhog
    Sep 5, 2016 at 12:18
  • see updated answer Sep 5, 2016 at 12:37
  • I have tried updating all instances of eav/entity_attribute_option_collection with the setPositionOrder but unfortunately there must be something deeper in the code setting the sort order. Thank you for your assistance.
    – hejhog
    Sep 5, 2016 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.