問題描述
在我的塊代碼中,我試圖以編程方式檢索具有特定值的屬性的產品列表.
In my block code I am trying to programmatically retrieve a list of products that have a attribute with a specific value.
或者,如果這是不可能的,那么如何檢索所有產品然后過濾它們以僅列出具有特定屬性的產品?
Alternately if that is not possible how would one retrieve all products then filter them to just list the products with a specific attribute?
我將如何使用標準布爾過濾器 AND
或 OR
執行搜索以匹配我的產品子集?
How would I perform a search using standard boolean filters AND
or OR
to match a subset of my products?
推薦答案
幾乎所有的 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());
}
這篇關于Magento - 檢索具有特定屬性值的產品的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!