简体   繁体   English

magento - 使用addAttributeToSort为自定义属性对产品集合进行排序 - 无法正常工作

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

I want to make the sorting via getLoadedProductCollection. 我想通过getLoadedProductCollection进行排序。

For product attribute, I have one custom attribute 'featured_product' 对于产品属性,我有一个自定义属性'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 转到Admin-> Catalog-> Attributes-> Manage Attributes,然后单击featured_product进行编辑,然后从编辑屏幕进行编辑

set Used in Product Listing = Yes set在产品列表中使用=是

Used for Sorting in Product Listing = Yes 用于产品列表中的排序=是

and save the attribute and clear the cache. 并保存属性并清除缓存。

if require than re-index the data. 如果需要,则重新索引数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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