久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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 - 檢索具有特定屬性值的產品的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 伊人国产精品 | 麻豆视频国产在线观看 | av在线黄 | 九九免费观看视频 | 欧美午夜久久 | 天天射视频| 99re热精品视频 | 欧美成年人视频在线观看 | 日韩中文字幕网 | 欧美亚洲国语精品一区二区 | 天堂久久av | 国产精品久久二区 | 日本高清不卡视频 | 国产免费观看一区 | 神马影院一区二区三区 | 亚洲男人的天堂网站 | 日韩一区二区在线视频 | 日韩免| 国产福利在线 | 五十女人一级毛片 | www.国产| 播放一级黄色片 | 亚洲欧美在线观看 | 9191成人精品久久 | 一区二区三区影院 | 激情91| 91精品久久久久久久久中文字幕 | 国产视频第一页 | 久久久国产一区 | 免费观看一区二区三区毛片 | 国产在线精品一区二区三区 | 久久最新网址 | 国产一级毛片精品完整视频版 | 久草视频网站 | 黄色在线观看网站 | 黄网站免费在线看 | 一级片av | 在线国产一区二区三区 | 国产成人精品免高潮在线观看 | 精品国产第一区二区三区 | 三级免费av |