0

In our magento 2.2.5 based website we want to display the layered navigation in alphabetical order. I found code online to do this but with no explanation on what to name the files or where to put the code (I believe it should go in app/code folder but...???

Override the Eav_Entity_Attribute_Source_Table model using this
 <global>
    <models>
        <eav>
           <rewrite>
              <entity_attribute_source_table>Namespace_Module_Model_Eav_Entity_Attribute_Source_Table</entity_attribute_source_table>
           </rewrite>
        </eav>
    </models>
 </global>

And then this PHP code

    Override the getAllOptions method as follows:
    <?php
        class Namespace_Module_Model_Eav_Entity_Attribute_Source_Table extends Mage_Eav_Model_Entity_Attribute_Source_Table
        {
           public function getAllOptions($withEmpty = true, $defaultValues = false)
             {
                 $storeId = $this->getAttribute()->getStoreId();
                 if (!is_array($this->_options)) 
                 {
                    $this->_options = array();
                 }
                 if (!is_array($this->_optionsDefault)) 
                 {
                    $this->_optionsDefault = array();
                 }
                 if (!isset($this->_options[$storeId])) 
                 {
                    $collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                    ->setAttributeFilter($this->getAttribute()->getId())
                    ->setStoreFilter($this->getAttribute()->getStoreId());

                    $collection->getSelect()->order(['main_table.sort_order asc', 'value asc']);
                    $collection->load();

                    $this->_options[$storeId]        = $collection->toOptionArray();
                    $this->_optionsDefault[$storeId] = $collection->toOptionArray('default_value');
               }



               $options = ($defaultValues ? $this->_optionsDefault[$storeId] : $this->_options[$storeId]);
               if ($withEmpty) 
               {
                  array_unshift($options, array('label' => '', 'value' => ''));
               }
               return $options;
          }
     }
  • All I can say is that you found the code for magent 1. This won't work for magento2. – Marius Apr 22 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.