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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)