久久久久久久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一区 | 狠狠色综合久久丁香婷婷 | 污书屋 | 亚洲第1页| 中文字幕在线观看一区 | 欧美aaaaaaaa | 国产成人jvid在线播放 | 天天久久 | 国产成人一区二区三区电影 | 黄网免费 | 国产精品高潮呻吟久久 | 国产精品三级久久久久久电影 | 91精品国产综合久久久久久蜜臀 | 久久国产欧美日韩精品 | 91精品国产综合久久精品 | 亚洲网址 | 国产在线高清 | 久久中文一区二区 | 久久日本| 在线一区 | 国产精品国产精品国产专区不卡 | 亚洲自拍偷拍欧美 | 欧美日韩久久精品 | 国色天香综合网 | www.久久 | 99re视频| 精品国偷自产在线 | 国产精品国产三级国产aⅴ原创 | 在线播放中文字幕 |