gpt4 book ai didi

php - 我如何在 Magento 中订购最新产品?

转载 作者:搜寻专家 更新时间:2023-10-31 21:44:50 26 4
gpt4 key购买 nike

我正在尝试在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com