简体   繁体   English

Magento产品集仅获取某些类别的产品

[英]Magento Product Collection Get Only Products From Certain Categories

I am trying to get a list of products that have a sale price that are only in certain categories. 我正在尝试获取仅在某些类别中具有销售价格的产品列表。 Right now I am trying to use a product collection to get this data. 现在,我正在尝试使用产品集合来获取此数据。 I am not sure how I would go about restricting the collection for particular categories only. 我不确定如何限制仅针对特定类别的收藏。 Here is what I have so far: 这是我到目前为止的内容:

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', 1)
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('special_price', array('neq' => ""))
    ->addAttributeToFilter('discontinued', array('neq' => 1))
    ->setPageSize(10)
    ->setOrder('price', 'ASC')
    ;

The discontinued attribute is a custom attribute that we use so that products don't display but also don't 404. 停产属性是我们使用的自定义属性,因此产品既不会显示,也不会显示404。

Is there a way to use the product model and restrict to certain categories? 有没有一种使用产品模型并将其限制为某些类别的方法?

Figured it out. 弄清楚了。 You start with the category and get the product collection from the category and then refine it down from there. 您从类别开始,然后从类别中获取产品集合,然后从那里进行优化。 In code it looks like this: 在代码中,它看起来像这样:

$products = Mage::getModel('catalog/category')->load(410)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', 1)
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('special_price', array('neq' => ""))
    ->addAttributeToFilter('discontinued', array('neq' => 1))
    ->setOrder('price', 'ASC')
    ;

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

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