在我的代碼塊中,我試圖以編程方式檢索具有特定值的屬性的產品列表。Magento - 檢索具有特定屬性值的產品
或者,如果這是不可能的,如何檢索所有產品,然後過濾它們只列出具有特定屬性的產品?
如何使用標準布爾篩選器AND
或OR
執行搜索以匹配我的產品子集?
在我的代碼塊中,我試圖以編程方式檢索具有特定值的屬性的產品列表。Magento - 檢索具有特定屬性值的產品
或者,如果這是不可能的,如何檢索所有產品,然後過濾它們只列出具有特定屬性的產品?
如何使用標準布爾篩選器AND
或OR
執行搜索以匹配我的產品子集?
幾乎所有的Magento模型都有一個對應的Collection對象,可以用來獲取模型的多個實例。
實例化一個產品集合,請執行下列操作
$collection = Mage::getModel('catalog/product')->getCollection();
產品Magento的EAV的風格模式,所以你需要在要返回任何額外的屬性添加。
$collection = Mage::getModel('catalog/product')->getCollection();
//fetch name and orig_price into data
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
在集合上設置過濾器有多種語法。我總是使用下面的詳細描述,但您可能想要檢查Magento源代碼以獲取更多使用過濾方法的方法。
下面介紹如何通過一個範圍值(比大於以下)過濾
$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'),
));
雖然這將等於一件事或其他的名稱進行篩選。
$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'),
));
支持的短條件句的完整列表(當量,LT等)可以在_getConditionSql
方法找到在lib/Varien/Data/Collection/Db.php
最後,所有的Magento集合可以遍歷(基集合類實現迭代器接口)。這是您設置過濾器後如何抓住您的產品。
$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('name'=>'orig_price','eq'=>'Widget A'),
array('name'=>'orig_price','eq'=>'Widget B'),
));
foreach ($collection as $product) {
//var_dump($product);
var_dump($product->getData());
}
這是跟我原來的問題,以幫助其他人有同樣的問題。如果您需要按屬性過濾,而不是手動查找id,則可以使用以下代碼檢索屬性的所有id,value對。數據以屬性名稱作爲鍵的數組返回。
function getAttributeOptions($attributeName) {
$product = Mage::getModel('catalog/product');
$collection = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', $attributeName);
$_attribute = $collection->getFirstItem()->setEntity($product->getResource());
$attribute_options = $_attribute->getSource()->getAllOptions(false);
foreach($attribute_options as $val) {
$attrList[$val['label']] = $val['value'];
}
return $attrList;
}
這裏是一個函數,你可以用它來獲取產品的屬性集id。使用上一個函數檢索。
function getProductsByAttributeSetId($attributeSetId) {
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('attribute_set_id',$attributeSetId);
$products->addAttributeToSelect('*');
$products->load();
foreach($products as $val) {
$productsArray[] = $val->getData();
}
return $productsArray;
}
我在
應用程序/代碼/核心/法師/目錄/座/產品/列表添加的行
$this->_productCollection->addAttributeToSelect('releasedate');
。在功能上線95
PHP _getProductCollection()
,然後調用它
應用程序/設計/前端/默認/ hellopress /模板/目錄/產品/ list.phtml
通過編寫代碼
<div><?php echo $this->__('Release Date: %s', $this->dateFormat($_product->getReleasedate())) ?>
</div>
現在它在Magento 1.4.x中工作
獲取TEXT
屬性從管理員添加到產品列表頁面的前端。
感謝Anita Mourya
我發現有兩種方法。假設從後端添加名爲「na_author」的產品屬性作爲文本字段。
方法1
上list.phtml
<?php $i=0; foreach ($_productCollection as $_product): ?>
對於每個產品LOAD BY SKU和GET ATTRIBUTE INSIDE FOREACH
<?php
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$_product->getSku());
$author = $product['na_author'];
?>
<?php
if($author!=""){echo "<br /><span class='home_book_author'>By ".$author ."</span>";} else{echo "";}
?>
方法2
Mage/Catalog/Block/Product/List.phtml
OVER騎在設定'本地文件夾'
即 複製自
Mage/Catalog/Block/Product/List.phtml
,並通過加入在下面粗體顯示2行粘貼到
app/code/local/Mage/Catalog/Block/Product/List.phtml
變化的功能。
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$layer = Mage::getSingleton('catalog/layer');
/* @var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
**//CMI-PK added na_author to filter on product listing page//
$this->_productCollection->addAttributeToSelect('na_author');**
return $this->_productCollection;
}
你會很高興看到它.... !!
$attribute = Mage::getModel('eav/entity_attribute')
->loadByCode('catalog_product', 'manufacturer');
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getData('attribute_id'))
->setStoreFilter(0, false);
$preparedManufacturers = array();
foreach($valuesCollection as $value) {
$preparedManufacturers[$value->getOptionId()] = $value->getValue();
}
if (count($preparedManufacturers)) {
echo "<h2>Manufacturers</h2><ul>";
foreach($preparedManufacturers as $optionId => $value) {
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('manufacturer');
$products->addFieldToFilter(array(
array('attribute'=>'manufacturer', 'eq'=> $optionId,
));
echo "<li>" . $value . " - (" . $optionId . ") - (Products: ".count($products).")</li>";
}
echo "</ul>";
}
創建屬性名稱爲「price_screen_tab_name
」。並使用這個簡單的公式訪問。
<?php $_product = $this->getProduct(); ?>
<?php echo $_product->getData('price_screen_tab_name');?>
非常詳細的答案。謝謝! – 2009-08-27 04:05:05
非常感謝您的詳細解答。你讓我走在正確的道路上。我做了你的示例代碼的結果var_dump。因爲我正在使用的屬性是一個多重選擇項目,所以我在結果中得到一個數字ID,所以文本比較不起作用。例如。$這 - >系列 - > addFieldToFilter(陣列( 陣列( '屬性'=> 'cw_category', '當量'=>的Aero'), 陣列( '屬性'=> 'cw_category', '當量'=> 'track'), array('attribute'=>'cw_category','eq'=>'Touring') )); 將返回 「cw_category」 =>字符串」,536535534' (長度= 12) – Christian 2009-08-27 08:01:49
不能專門幫助你有沒有大量的挖掘(StackOverflow的代表是好的,但它並沒有支付賬單)。兩種途徑讓你去追求。首先,如上所述,檢查_getConditionSql以獲取所有可能的比較運算符的列表。你也許可以用類似條款或可能的在獲得通過。其次,如果你簽出PHPDoc的對Mage_Eav_Model_Entity_Collection_Abstract的addAttributeToFilter方法,你會發現第一個參數的預期值之一是Mage_Eav_Model_Entity_Attribute_Interface。這可能會導致你走上正確的道路。 – 2009-08-27 17:39:05