6
votes

I have products collection.

$_products = Mage::getModel('catalog/product')->getCollection();

In admin panel in Catalog->Manage Categories->Category products i have position for each product. How i can sort $_products by position ?

3

3 Answers

5
votes

If you want to sort products by position in category $category_id. you can use the following

//Load the category model

 $category = Mage::getModel('catalog/category')->load($category_id)
             ->getProductCollection()
             ->addAttributeToSort('position', 'ASC');

$products_list = $category->getData();

you will get all products sorted by position in that category $category_id

2
votes

you need to add category, where you set positions for products, as filter for collection

$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($category);
$collection->addAttributeToSort('position', 'ASC');
0
votes

I have created separated extension which create new products attribute with name 'position'. In this extension i have create observer which listening category save event. So when admin save category in my observer i get each position and set my attribute 'position' for each product. And at at last i can sort products collection by 'position'(product) attribute.