简体   繁体   English

Magento产品集合:每个自定义属性输出前3种产品

[英]Magento product collection: Output top 3 products per custom attribute

I have created a custom attribute "quality_level" indicating three levels of quality: entrance, standard, premium. 我创建了一个自定义属性“ quality_level”,该属性指示了三个质量级别:入口,标准,高级。

I am now trying to create category pages, which should fully work with the layered navigation, that lists just the top 3 products for each quality level (ie up 3x3 products). 我现在正在尝试创建类别页面,该页面应该与分层导航完全兼容,该页面仅列出每个质量级别的前3个产品 (即3x3产品)。

The sorting for what is regarded "top 3" is based on another custom attribute "quality_measure", which is defined between 1 (lowest) and 1000 (best). 所谓“前3名”的排序基于另一个自定义属性“ quality_measure”,该属性定义在1(最低)和1000(最佳)之间。

I tried to implement it as follows: 我尝试如下实现:

$_productCollectionTmp = clone $this;
$_productCollection1 = $_productCollectionTmp
    ->getProductCollection()
    ->clear()
    ->addAttributeToFilter('quality_level', array('in' => array(305)))
    ->addAttributeToSort('quality_measure', 'DESC')
    ->load();

$_productCollectionTmp = clone $this;
$_productCollection2 = $_productCollectionTmp
    ->getProductCollection()
    ->clear()
    ->addAttributeToFilter('quality_level', array('in' => array(306)))
    ->addAttributeToSort('quality_measure', 'DESC')
    ->load();

...

However, this does not only feel pretty inefficient, it also results in actually no product being displayed at all as the addAttributeToFilter seems to apply to all product collections I am trying to clone. 但是,这不仅效率低下,而且实际上实际上根本没有显示任何产品,因为addAttributeToFilter似乎适用于我要克隆的所有产品集合。

Alternatively, I thought about a more efficient way with an additional WHERE clause, similar to this example here: http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ (example under "Select the top N rows from each group"). 另外,我想到了一个额外的WHERE子句的更有效方法,类似于此处的示例: http : //www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row -per-group-in-sql / (例如“从每个组中选择前N行”下的示例)。 However, I couldn't figure out how to operationalize this in my case. 但是,我无法弄清楚该如何实现。

Could I get my result with a clever statement like this? 我能用这样一个聪明的陈述得到我的结果吗?

... ->getSelect()->where("(SELECT count(*) from ...) <= 3") ...

Any ideas or suggestions? 有什么想法或建议吗?

You are doing it almost OK. 您这样做几乎可以。
The problem is that once you call load on a collection the items are retrieved from the db. 问题在于,一旦您调用集合上的load ,就会从数据库中检索项目。
After that calling reset will not make the collection load again. 之后,调用reset将不会再次加载集合。
So try this code: 因此,请尝试以下代码:

$_productCollectionTmp = clone $this;
$_productCollection1 = $_productCollectionTmp
    ->getProductCollection()
    ->clear() //not sure if this is needed anymore
    ->addAttributeToFilter('quality_level', array('in' => array(305)))
    ->addAttributeToSort('quality_measure', 'DESC')
;

$_productCollectionTmp = clone $this;
$_productCollection2 = $_productCollectionTmp
    ->getProductCollection()
    ->clear() //not sure if this is needed anymore
    ->addAttributeToFilter('quality_level', array('in' => array(306)))
    ->addAttributeToSort('quality_measure', 'DESC')
;

Then you just need to loop through $_productCollection1 and $_productCollection2 . 然后,您只需要遍历$_productCollection1$_productCollection2 The load will be automatically called when doing the foreach loop. 在执行foreach循环时,将自动调用该load

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

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