我正在尝试在我的 magento 商店中展示最新产品。
什么是最近的产品?它由两个标准定义:
- 创建于
- news_from_date
我想在 created_at 和 news_from_date 之间无差异地订购我的产品,而不是先按一个标准,再按第二个标准。
有什么想法吗?
我已经尝试过以下代码:
->addAttributeToSort('created_at, news_from_date', 'desc')
或
->addAttributeToSort('news_from_date', 'desc')
->addAttributeToSort('created_at', 'desc')
或
->addAttributeToSort(array('created_at' => 'desc', 'news_from_date' => 'desc'))
或
->addAttributeToSort(array('created_at', 'news_from_date'), 'desc'))
最佳答案
SQL 顺序子句中没有“OR”,因此您需要找到一种方法将问题减少到单个列。以下内容未经测试,但应该会给您一个想法。
// Make sure the correct attributes are available with names we choose.
$products->addAttributeToSelect('created_at, news_from_date');
// Choose the LATEST date to sort as it is the most RECENT.
$products->addOrder('MAX(created_at, news_from_date)', 'desc');
关于php - 我如何在 Magento 中订购最新产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5356610/