简体   繁体   English

Magento,按“是/否”属性排序

[英]Magento, sort by YES/NO attribute

I have to load a collection of products and sort them for further display. 我必须加载产品集合并对其进行分类以进一步显示。

Originally the collection is sorted by date. 最初,该集合按日期排序。 That works flawlessly. 完美无瑕。 Now I had to add a new YES/NO attribute to the products. 现在,我必须向产品添加新的是/否属性。 That worked out great as well. 效果也很好。

The only thing I wasn't able to figure out is how to sort my collection by this attribute. 我唯一无法弄清楚的是如何通过此属性对集合进行排序。 The collection would have to be sorted by this new attribute as well as by the date. 该集合必须按此新属性以及日期进行排序。

I thought this would work: 我认为这可以工作:

$collection->addAttributeToSelect('name');

$collection->addAttributeToSelect('YesNoAttr');

$collection->addAttributeToSelect('special_to_date');

$collection->addAttributeToSelect('short_description');

$collection->addAttributeToSelect('image');

$collection->addAttributeToSort('YesNoAttr', 'ASC')->addAttributeToSort('special_to_date', 'ASC');

$products = $collection->load();

It doesn't. 没有。 I also tried to set the order: 我还尝试设置顺序:

$collection->setOrder('YesNoAttr','ASC');

I tried sorting only by the new attribute, but it didn't work either, so I'm pretty sure it has nothing to do with me trying to sort by 2 attributes. 我尝试仅按new属性进行排序,但也无法正常工作,因此我很确定它与按2个属性进行排序无关。

I am trying to solve this now for hours and I'm completely clueless what I'm doing wrong. 我试图解决这个问题已经有几个小时了,但我完全不知道自己在做什么错。

Any help would be greatly appreciated. 任何帮助将不胜感激。

To add more than one attribute to order you can try to do this: 要向订单添加多个属性,您可以尝试执行以下操作:

$collection->getSelect()->order('main_table.YesNoAttr asc')
        ->order('main_table.special_to_date asc');

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

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