Magento - How to sort search attributes in advanced search by position?

Posted by Damodar Bashyal on June 19, 2012

 

As I was upgrading magento for a client I found that by default magento sorts search filters by attribute_id. Old website had different attribute_id than that in new website. So, the filters were displaying in different position in new website than that of old website. I wanted to keep it as the old one, so I updated the position of attributes but that made no difference.

After going through code found the code that pulls attributes:

app\code\core\Mage\CatalogSearch\Model\Advanced.php

/**
  * Retrieve array of attributes used in advanced search
  *
  * @return array
  */
  public function getAttributes()
  {
    /* @var $attributes Mage_Catalog_Model_Resource_Eav_Resource_Product_Attribute_Collection */
    $attributes = $this->getData('attributes');
    if (is_null($attributes)) {
        $product = Mage::getModel('catalog/product');
        $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
        ->addHasOptionsFilter()
        ->addDisplayInAdvancedSearchFilter()
        ->addStoreLabel(Mage::app()->getStore()->getId())
        ->setOrder('main_table.attribute_id', 'asc')
        ->load();
        foreach ($attributes as $attribute) {
            $attribute->setEntity($product->getResource());
        }
        $this->setData('attributes', $attributes);
    }
    return $attributes;
  }

I printed the SQL query with:

$attributes->printLogQuery(true);

I got the sql query that was used to pull the attributes as below.

SELECT `main_table`.`entity_type_id`, `main_table`.`attribute_code`, `main_table`.`attribute_model`, `main_table`.`backend_model`, `main_table`.`backend_type`, `main_table`.`backend_table`, `main_table`.`frontend_model`, `main_table`.`frontend_input`, `main_table`.`frontend_label`, `main_table`.`frontend_class`, `main_table`.`source_model`, `main_table`.`is_required`, `main_table`.`is_user_defined`, `main_table`.`default_value`, `main_table`.`is_unique`, `main_table`.`note`, `additional_table`.*, `ao`.`option_id`, IFNULL(al.value, main_table.frontend_label) AS `store_label` FROM `eav_attribute` AS `main_table` INNER JOIN `catalog_eav_attribute` AS `additional_table` ON additional_table.attribute_id = main_table.attribute_id LEFT JOIN `eav_attribute_option` AS `ao` ON ao.attribute_id = main_table.attribute_id LEFT JOIN `eav_attribute_label` AS `al` ON al.attribute_id = main_table.attribute_id AND al.store_id = 1 WHERE (main_table.entity_type_id = 4) AND ((main_table.frontend_input = 'select' AND ao.option_id > 0) OR (main_table.frontend_input <> 'select') OR (main_table.is_user_defined = 0)) AND (additional_table.is_visible_in_advanced_search = '1') GROUP BY `main_table`.`attribute_id` ORDER BY main_table.attribute_id ASC

After going through SQL query and tables used, I found the 'position' column was in table catalog_eav_attribute which was referenced as 'additional_table'. So, I override the function with my local and set the sorting to be done by using position.

app\code\local\Technooze\CatalogSearch\Model\Advanced.php

/**
  * Retrieve array of attributes used in advanced search
  *
  * @return array
  */
  public function getAttributes()
  {
    /* @var $attributes Mage_Catalog_Model_Resource_Eav_Resource_Product_Attribute_Collection */
    $attributes = $this->getData('attributes');
    if (is_null($attributes)) {
        $product = Mage::getModel('catalog/product');
        $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
        ->addHasOptionsFilter()
        ->addDisplayInAdvancedSearchFilter()
        ->addStoreLabel(Mage::app()->getStore()->getId())
        //->setOrder('main_table.attribute_id', 'asc')
        ->setOrder('additional_table.position', 'asc')
        ->load();
        foreach ($attributes as $attribute) {
            $attribute->setEntity($product->getResource());
        }
        $this->setData('attributes', $attributes);
    }
    return $attributes;
  }

Finally I was able to move around the position of search filters up and down easily.

Sarvesh V Tiwari posted on - Wednesday 31st of May 2017 01:09:21 AM

First of all i would like to you thank you for sharing answer

its very good answer
 
not published on website


QR Code: Magento - How to sort search attributes in advanced search by position?