简体   繁体   中英

magento - sort product collection by using addAttributeToSort for custom attribute - not working

I want to make the sorting via getLoadedProductCollection.

For product attribute, I have one custom attribute 'featured_product'

Here is my code:

$sort=$_GET['s'];
$_productCollection=$this->getLoadedProductCollection()->addAttributeToSelect('featured_product');

$_productCollection->clear();
$_productCollection->getSelect()->reset(Zend_Db_Select::ORDER);

switch($sort)
{
    case 'lp':
    $_productCollection->addAttributeToSort('price', 'ASC');
    break;
    case 'hp':
    $_productCollection->addAttributeToSort('price', 'DESC');
    break;
    case 'fp':
    $_productCollection->addAttributeToSort('featured_product');

    break;
}

For first 2 cases, it works with no problems.

But the third one is not working at all.

I want to move all the featured products to the beginning of the collection list.

How can I change the code for achieving the third case?

Thanks

I feel you need to work with admin also.

go to Admin->Catalog->Attributes->Manage Attributes than click on featured_product to edit it and from edit screen

set Used in Product Listing = Yes

Used for Sorting in Product Listing = Yes

and save the attribute and clear the cache.

if require than re-index the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM