Magento: sorting a collection of products

I am creating a template to display recognized products on the homepage, and I would like to control the order of the products.

This is what I am using at the moment to get a collection of products based on the category:

<?php $_productCollection = $this->getLoadedProductCollection(); ?> 

There is no specific sorting at all.

When I was going to sort the products, I expected this to work:

 <?php $_productCollection = $this->getLoadedProductCollection()->addAttributeToSort('name', 'ASC'); ?> 

But there is no difference. What am I doing wrong?

Thank you in advance!

+6
source share
1 answer

use this one with which i worked just like try.

 $collection = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC); 

in decreasing order:

 $collection = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_DESC); 

for a product with its category:

 $collection = Mage::getModel('catalog/category')->load($categoryId) ->getProductCollection() ->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC); 

Or you can find more help on the magento wiki .

+8
source

Source: https://habr.com/ru/post/902779/


All Articles