简体   繁体   English

自定义排序的Magento集合?

[英]Custom Sort of Magento Collection?

Here's the thing, I have product collection that I need to sort by their size. 事情就是这样,我需要按商品的大小对产品系列进行分类。 The size attribute values are XS, S, M, L, XL. 大小属性值为XS,S,M,L,XL。 When I put simply 当我简单地说

addAttributeToSort('size', 'DESC')

it sorts them by alphabetical order of size attribute values (XS, XL, S, M, L). 它按大小属性值(XS,XL,S,M,L)的字母顺序对其进行排序。

Does anyone have any tip how to solve this? 有没有人提示如何解决这个问题?

Best I could think of is to create some new select field during the select process which will, through case command, have values 1,2,3,4,5, depending on the size value, and the sort it by that. 我能想到的最好的方法是在选择过程中创建一些新的选择字段,根据大小值,通过case命令将其值分别为1,2,3,4,5,并对其进行排序。 But I don't know how to do that, or if it's even possible in Magento. 但是我不知道该怎么做,或者甚至在Magento中也不可能。

The fastest hack for this would be to have a second attribute that defines a proper sort order for the attributes, which I think you may have been referring to in your last paragraph. 最快的解决方法是拥有第二个属性,为该属性定义正确的排序顺序,我认为您可能在上一段中已经提到过。 That would let you sort on that attribute itself. 这样您就可以对该属性本身进行排序。 Of course, this requires extra data input to make work in your catalog. 当然,这需要额外的数据输入才能使您的目录生效。

Another thing you could try (I honestly don't know if this will work) is attempt to work with the admin/store level labels for your attribute. 您可以尝试做的另一件事(老实说,我不知道这是否可行)是尝试使用属性的管理/存储级别标签。 It's possible that Magento uses one over the other for sorting, though I am somewhat skeptical. Magento可能会使用一个进行排序,尽管我有些怀疑。

Hope that helps! 希望有帮助!

Thanks, Joe 谢谢乔

Interesting question. 有趣的问题。 You'll notice from my earlier question on sorting that Magento uses SQL ORDER BY statements for sorting by default. 您会从前面有关排序的问题中注意到,Magento默认使用SQL ORDER BY语句进行排序。 That will make it pretty hard to achieve what you need (IMHO) using the addAttributeToSort() approach. 这将使使用addAttributeToSort()方法很难实现所需的内容(IMHO)。

You could use a similar approach to the answer that I posted to that question which basically converts the collection to an array of objects, then applies a custom sort function. 您可以对我发布给该问题的答案使用类似的方法,该方法基本上将集合转换为对象数组,然后应用自定义排序功能。

You need to create your own custom module and update your layout xml to point at the new block eg 您需要创建自己的自定义模块并更新布局xml以指向新块,例如

<block type="navigation/dropdown" name="product_list" template="catalog/product/list_dropdown.phtml"/>

Hope this helps, 希望这可以帮助,
JD JD

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

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