Hi I have created one attribute called archive whose type is Yes/No. So archive attribute for every product has either Yes/No.

Now I want to sort product collection so that all products whose archive value=YES should come at the end.

I tried:

$collection = Mage::getModel('catalog/product')->getCollection()
             ->addAttributeToFilter('type_id', 'configurable')
             ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
             ->addAttributeToFilter('category_id', array(
                 array('finset' => '37'))
             ->addAttributeToSort('archive', 'desc');

But it does not work.

Printing $collection->getSelect() results in:

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `price_index`.`price`,    
`price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS  
NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) 
AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`,  
`price_index`.`tier_price` FROM `catalog_product_entity` AS `e` INNER JOIN  
`catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND 
cat_index.store_id='1' AND cat_index.category_id = '37' INNER JOIN 
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id 
AND price_index.website_id = '1' AND price_index.customer_group_id = 0 WHERE 
(`e`.`type_id` = 'configurable')
  • how's it getting sorted now?
    – Shathish
    Dec 28 '13 at 4:40
  • Try to print your SQL query and check. print $collection->getSelect();
    – Sukeshini
    Dec 28 '13 at 10:01

Make sure the archive attribute is added to the flat product data by setting Used in Product Listing to yes. Also it's probably better to load the product collection via the catalog/category model instead of joining another table.

Also, to have yes come as last make the order ascending instead of descending

$category = Mage::getModel('catalog/category')->load(37);
$collection = $category->getProductCollection()
   ->addAttributeToFilter('type_id', 'configurable')
   ->addAttributeToSort('archive', 'asc');
  • where I do this modify? Jun 9 '16 at 10:14
  • @ClaudioPomo can you be more specific? You mean where you set the attribute flat data property?
    – Sander Mangel
    Jun 10 '16 at 6:29
  • Where is the file to update? Or in which part of magento core I have to edit? Jun 10 '16 at 6:33
  • @ClaudioPomo This was actually an answer to some script OP wrote. Why not post your exact question with case and I promise to take a look :)
    – Sander Mangel
    Jun 10 '16 at 7:43
  • This is my question Jun 10 '16 at 7:56

May once you check with attribute configurations in magento like website,Used for Sorting in Product Listing few of the setting will influence collection...try for luck

Your Answer

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

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