andy1786
andy1786

Reputation: 131

How can I sort a loaded product collection in Magento?

I want to sort a product collection that is already loaded by

$_productCollection = $this->getLoadedProductCollection();

The default sort in admin Magento is Style attribute

I want to sort first by Style, then by color and then by name.

I've try

$_productCollection->setOrder(array('style', 'color','name'), asc);

and also

$_productCollection->addAttributeToSort('color', Varien_Data_Collection::SORT_ORDER_ASC);
$_productCollection->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC);

but is not working.

The default sort is working good. Can someone please help?

Upvotes: 6

Views: 6163

Answers (6)

AndyS
AndyS

Reputation: 1

Have it working in v2.3.7 using the following

$_productCollection = $block->getLoadedProductCollection();
$_productCollection->getSelect()->reset(Zend_Db_Select::ORDER);
$_productCollection->setOrder('price', 'ASC');

Upvotes: 0

Vivek Kumar
Vivek Kumar

Reputation: 36

You can use setOrder on collection to sort any collection data like below :

$_productCollection->setOrder('id','DESC');

Where id can be replace with your column name, & second parameter can be DESC & ASC as per your requirement.

Upvotes: 0

Magnus
Magnus

Reputation: 31

If you have products in a collection that need more advanced sorting/oordering you can move the products to an assoc array which is much easier to sort/order as you like. Once the order is as you like you can add them back by first clearing your collection with $collection-removeAllItems() and then iterate your array as $product and move each product back to the collection with $collection->addItem($product);

I used this when I wanted a sticky that was not touched by different ordering/sorting.

Upvotes: 0

00-BBB
00-BBB

Reputation: 856

Nothing here or elsewhere worked for me. No matter what I tried it would just not sort on /Magento_Catalog/templates/product/list.phtml using 'getLoadedProductCollection()'. Also had a weird issue where products would disappear after clearing cache, until a reindex was done. No idea what was going on there, something was not right.

So I got it to work in the hacky way below (Yes I know it's not 'best practice' to use object manager but it was the only thing that worked!!)

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryId = 2;
$category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
$productCollection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$_productCollection = $productCollection->create()
    ->addAttributeToSelect('*')
    ->setOrder('position', 'ASC')
    ->addCategoryFilter($category)->load();

Upvotes: 0

Rohit Pareek
Rohit Pareek

Reputation: 1563

You can also take clone of the loaded collection and then modify the query as you want.

// clone of the current collection.Better way of modifying the colection

$_productCollection = clone $this->getLoadedProductCollection();

// unset the cuurent coolection and append your custom filter.

$_productCollection->clear()
               ->addAttributeToFilter('color', Varien_Data_Collection::SORT_ORDER_ASC)
               ->load();

Upvotes: 0

Cristiano Casciotti
Cristiano Casciotti

Reputation: 1026

You can do it this way:

$_productCollection = $this->getLoadedProductCollection();

$_productCollection->clear();
$_productCollection->addAttributeToSort('color', Varien_Data_Collection::SORT_ORDER_ASC);

foreach ($_productCollection as $product) {
    // ...
}

This way the collection is forced to be reloaded and then your custom sorting is applied.

Upvotes: 7

Related Questions