小编Man*_*uel的帖子

如何使用ORDER BY FIELD对magento产品集合进行排序

我有一个自定义产品属性,实际上有三个可能的值"Frühjahr/ Sommer","Herbst/Winter",""(空).

我希望按照预定的顺序按此值对产品集合进行排序,例如"Frühjahr/ Sommer","Herbst/Winter",""或"Herbst/Winter","Frühjahr/ Sommer",""

这是交替的.空值应该在结尾处,但可能还有更多值,因此它必须是固定的预定义顺序.

我需要执行以下MySQL ORDER BY FIELD(saison,"Frühjahr/ Sommer","Herbst/Winter","")

问题是我不知道如何在Magento中执行此命令.我知道" - > setOrder"-Method但我需要提交一个固定的顺序而不是使用DESC或ASC.

我在stackoverflow和google上搜索了很多但到目前为止还没有答案.

我希望你能帮帮我

谢谢

[编辑回应magalter的答案]

不幸的是它不起作用我之前尝试过:

$this->_collection->getSelect()->order(new Zend_Db_Expr("FIELD(season, 'Frühjahr/Sommer','Herbst/Winter','')"));
Run Code Online (Sandbox Code Playgroud)

结果是"处理您的请求时出错.

$this->_collection->getSelect();
Run Code Online (Sandbox Code Playgroud)

返回:

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.visibility IN(2, 4) AND cat_index.category_id='124' INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id …
Run Code Online (Sandbox Code Playgroud)

magento

4
推荐指数
2
解决办法
3万
查看次数

标签 统计

magento ×1