1

我确信这可以通过一些聪明的 mysql 表达式或 zend 连接来解决,但我只是没有足够的知识来解决,希望 SO 社区中的某个人可以。

假设我在 Magento 中有一系列产品,它们是按类别位置排序的。

我只想从集合中检索 2 个产品,即类别位置最高的产品。

以下应说明:

Category:
    Product A (position 3);
    Product B (position 1);
    Product C (position 0);
    Product D (position 2);

在这个集合中,我只想检索产品 C 和 B,因为这是两个最高位置。

另一个插图:

Category:
    Product A (position 1);
    Product B (position 3);
    Product C (position 0);
    Product D (position 2);

在这个集合中,我只想检索产品 C 和 A。

如何在单个集合负载中完成此操作。例如,我不想加载集合然后迭代并手动排序等。

4

1 回答 1

0
$_category = Mage::getModel('catalog/category')->load($categoryId);
$coll = $_category->getProductCollection()->addAttributeToSort('position','ASC');
$coll->getSelect()->limit(2);
$coll->load();

之后,您可以遍历您的集合:

foreach ($coll as $product) {
   // Do sth. with $product
}
于 2013-01-09T10:38:22.370 回答