且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento - 检索具有特定属性值的产品

更新时间:2023-01-17 18:19:48

几乎所有的 Magento Models 都有一个对应的 Collection 对象,可以用来获取一个 Model 的多个实例.

Almost all Magento Models have a corresponding Collection object that can be used to fetch multiple instances of a Model.

要实例化产品集合,请执行以下操作

To instantiate a Product collection, do the following

$collection = Mage::getModel('catalog/product')->getCollection();

产品是 Magento EAV 样式模型,因此您需要添加要返回的任何其他属性.

Products are a Magento EAV style Model, so you'll need to add on any additional attributes that you want to return.

$collection = Mage::getModel('catalog/product')->getCollection();

//fetch name and orig_price into data
$collection->addAttributeToSelect('name');  
$collection->addAttributeToSelect('orig_price');    

有多种语法可用于在集合上设置过滤器.我总是使用下面的详细说明,但您可能需要检查 Magento 源以了解可以使用过滤方法的其他方式.

There's multiple syntaxes for setting filters on collections. I always use the verbose one below, but you might want to inspect the Magento source for additional ways the filtering methods can be used.

以下显示了如何按值范围(大于和小于)过滤

The following shows how to filter by a range of values (greater than AND less than)

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');  
$collection->addAttributeToSelect('orig_price');    

//filter for products whose orig_price is greater than (gt) 100
$collection->addFieldToFilter(array(
    array('attribute'=>'orig_price','gt'=>'100'),
)); 

//AND filter for products whose orig_price is less than (lt) 130
$collection->addFieldToFilter(array(
    array('attribute'=>'orig_price','lt'=>'130'),
));

虽然这将按等于一件事或另一件事的名称进行过滤.

While this will filter by a name that equals one thing OR another.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');  
$collection->addAttributeToSelect('orig_price');    

//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addFieldToFilter(array(
    array('attribute'=>'name','eq'=>'Widget A'),
    array('attribute'=>'name','eq'=>'Widget B'),        
));

可以在 lib/Varien/Data/Collection/Db.php 中的 _getConditionSql 方法中找到支持的短条件(eq、lt 等)的完整列表代码>

A full list of the supported short conditionals (eq,lt, etc.) can be found in the _getConditionSql method in lib/Varien/Data/Collection/Db.php

最后,所有 Magento 集合都可以迭代(基集合类实现了迭代器接口).这是设置过滤器后获取产品的方式.

Finally, all Magento collections may be iterated over (the base collection class implements on of the the iterator interfaces). This is how you'll grab your products once filters are set.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');  
$collection->addAttributeToSelect('orig_price');    

//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addFieldToFilter(array(
    array('attribute'=>'name','eq'=>'Widget A'),
    array('attribute'=>'name','eq'=>'Widget B'),        
));

foreach ($collection as $product) {
    //var_dump($product);
    var_dump($product->getData());
}