herveguetin
4/23/2014 - 6:56 PM

Bugfix - Make sure to sort filter options by sort order set in Magento attribute option edit

Bugfix - Make sure to sort filter options by sort order set in Magento attribute option edit

<?php
/**
 * @return array
 */
public function getAllFilterableOptionsAsHash()
{
	if (is_null($this->_optionsHash)) {
		$hash = array();
    $attributes = $this->getFilterableAttributes();
    
    /* @var $helper Amasty_Shopby_Helper_Url */
    $helper = Mage::helper('amshopby/url');
    
    $options = $this->getAllOptions();
    
    foreach ($attributes as $a){
        $code        = $a->getAttributeCode();
        $code = str_replace(array('_', '-'), Mage::getStoreConfig('amshopby/seo/special_char'), $code);
        $hash[$code] = array();
        foreach ($options as $o){
            if ($o['value'] && $o['attribute_id'] == $a->getId()) { // skip first empty
                $unKey = $helper->createKey($o['value']);
                while(isset($hash[$code][$unKey])){
                    $unKey .= '_';
                }

                
                
                
                // Hervé Guétin --> Bugfix - Make sure options are sorted by position set in admin
                //$hash[$code][$unKey] = $o['option_id'];
                $hash[$code][$unKey] = array('option_id' => $o['option_id'], 'sort_order' => $o['sort_order']);
                // [end] Hervé Guétin

                
                
                
                /*
                 * Keep original label for further use
                 */
                $this->_optionsLabels[$o['option_id']] = $o['value'];
            }
        }
    }

    
    
    
    // Hervé Guétin --> Bugfix - Make sure options are sorted by position set in admin
    //$this->_optionsHash = $hash;
    $newHash = array();
    foreach($hash as $attributeCode => $hash) {
        uasort($hash, function($a, $b) {
            if ($a['sort_order'] == $b['sort_order']) return 0;
            return ($a['sort_order'] > $b['sort_order'] ? 1 : -1);
        });
        foreach($hash as $optionLabel => $option) {
            $newHash[$attributeCode][$optionLabel] = $option['option_id'];
        }
    }
    $this->_optionsHash = $newHash;
    // [end] Hervé Guétin
    
    
    
	}

	return $this->_optionsHash;
}