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

Magento - Retrieve products with a specific attribute value(Magento - 检索具有特定属性值的产品)
本文介绍了Magento - 检索具有特定属性值的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我的块代码中,我试图以编程方式检索具有特定值的属性的产品列表.

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?

我将如何使用标准布尔过滤器 ANDOR 执行搜索以匹配我的产品子集?

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 - 检索具有特定属性值的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)