2009-08-26 44 views
76

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

或者,如果这是不可能的,如何检索所有产品,然后过滤它们只列出具有特定属性的产品?

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

回答

160

几乎所有的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()); 
} 
+5

非常详细的答案。谢谢! – 2009-08-27 04:05:05

+0

非常感谢您的详细解答。你让我走在正确的道路上。我做了你的示例代码的结果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

+0

不能专门帮助你有没有大量的挖掘(StackOverflow的代表是好的,但它并没有支付账单)。两种途径让你去追求。首先,如上所述,检查_getConditionSql以获取所有可能的比较运算符的列表。你也许可以用类似条款或可能的在获得通过。其次,如果你签出PHPDoc的对Mage_Eav_Model_Entity_Collection_Abstract的addAttributeToFilter方法,你会发现第一个参数的预期值之一是Mage_Eav_Model_Entity_Attribute_Interface。这可能会导致你走上正确的道路。 – 2009-08-27 17:39:05

7

这是跟我原来的问题,以帮助其他人有同样的问题。如果您需要按属性过滤,而不是手动查找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; 
} 
0

我在

应用程序/代码/核心/法师/目录/座/产品/列表添加的行

$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中工作

3

获取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; 

} 

你会很高兴看到它.... !!

5
$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>"; 
} 
2

创建属性名称为“price_screen_tab_name”。并使用这个简单的公式访问。

<?php $_product = $this->getProduct(); ?> 
<?php echo $_product->getData('price_screen_tab_name');?> 
相关问题