【问题标题】:Magento - Retrieve products with a specific attribute valueMagento - 检索具有特定属性值的产品
【发布时间】:2009-08-26 06:41:00
【问题描述】:

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

如果不可能的话,如何检索所有产品然后过滤它们以仅列出具有特定属性的产品?

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

【问题讨论】:

    标签: php magento e-commerce entity-attribute-value


    【解决方案1】:

    几乎所有 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'),        
    ));
    

    可在lib/Varien/Data/Collection/Db.php 中的_getConditionSql 方法中找到支持的短条件(eq、lt 等)的完整列表

    最后,所有 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('attribute'=>'name','eq'=>'Widget A'),
        array('attribute'=>'name','eq'=>'Widget B'),        
    ));
    
    foreach ($collection as $product) {
        //var_dump($product);
        var_dump($product->getData());
    }
    

    【讨论】:

    • 非常感谢您的详细解答。你让我走上了正确的道路。我对您的示例代码的结果做了一个 var_dump 。因为我正在使用的属性是一个多选项目,所以我在结果中得到了一个数字 id,所以文本比较不起作用。例如。 $this->collection->addFieldToFilter(array(array('attribute'=>'cw_category','eq'=>'Aero'),array('attribute'=>'cw_category','eq'=>'Track '), array('attribute'=>'cw_category','eq'=>'Touring') ));正在返回 'cw_category' => 字符串 ',536,535,534' (length=12)
    • 在没有大量挖掘的情况下无法专门为您提供帮助(StackOverflow 代表很好,但它不支付账单)。两条路供你追求。首先,如前所述,检查 _getConditionSql 以获取所有可能的比较运算符的列表。您可能可以使用 like 子句或 in。其次,如果您在 PHPDoc 中查看 Mage_Eav_Model_Entity_Collection_Abstract 上的 addAttributeToFilter 方法,您会看到第一个参数的预期值之一是 Mage_Eav_Model_Entity_Attribute_Interface。这可能会引导你走上正确的道路。
    • 在最终的代码示例中,您的数组以 'name' => 'orig_price' 开头 - 这是正确的吗?不应该是'attribute' => 'name'吗?
    • @johnsnails 这很有可能,这个帖子在 9 年前。我的回答可能包含不准确的信息,或者 API 可能已经从它下面改变了。这些天我没有做足够的 Magento 工作来确定。
    • @AlanStorm 看起来这只是您答案中另一个代码示例的复制/粘贴错误。我将编辑答案并纠正它。
    【解决方案2】:

    这是对我最初的问题的跟进,旨在帮助其他有同样问题的人。如果您需要按属性过滤,而不是手动查找 id,您可以使用以下代码检索属性的所有 id、值对。数据以数组的形式返回,属性名称为键。

    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;
    }
    

    【讨论】:

      【解决方案3】:
      $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>";
      }
      

      【讨论】:

        【解决方案4】:

        获取从管理员添加到产品列表页面前端的TEXT 属性。

        谢谢安妮塔·莫利亚

        我发现有两种方法。假设从后端添加名为“na_author”的产品属性作为文本字段。

        方法一

        list.phtml

        <?php $i=0; foreach ($_productCollection as $_product): ?>
        

        按 SKU 加载每个产品并在 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 RIDE 并设置在“本地文件夹”中

        即 复制自

        Mage/Catalog/Block/Product/List.phtml
        

        然后粘贴到

        app/code/local/Mage/Catalog/Block/Product/List.phtml
        

        通过添加下面以粗体显示的 2 行来更改功能。

        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】:

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

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

          【讨论】:

            【解决方案6】:

            我添加了一行

            $this->_productCollection->addAttributeToSelect('releasedate');
            

            app/code/core/Mage/Catalog/Block/Product/List.php 第 95 行

            在函数_getProductCollection()

            然后在里面调用它

            app/design/frontend/default/hellopress/template/catalog/product/list.phtml

            通过编写代码

            <div><?php echo $this->__('Release Date: %s', $this->dateFormat($_product->getReleasedate())) ?>
            </div>
            

            现在它在 Magento 1.4.x 中运行

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多