【问题标题】:Magento: Adding filterable attribute list to template fileMagento:将可过滤属性列表添加到模板文件
【发布时间】:2010-09-30 03:29:40
【问题描述】:

我在 Magento 的同一网站下设置了男士和女士商店。男性网站从男性类别运行,女性网站从女性类别运行。我有一个名为“设计师”的可过滤自定义属性设置。目前,类别页面上的分层导航显示该商店中产品的 Designer 属性以及与产品相关的任何设计师。我想在另一个页面上显示此列表。

我想将显示在分层导航中的设计器列表放入模板文件中。这个想法是,用户将来到我的网站,并希望查看我库存中的所有设计师的列表,具体取决于他们正在查看的商店。对于男士商店,他们会看到所有男士设计师的库存清单,对于女士商店也是如此。从这个设计师页面,他们可以选择最喜欢的设计师并购买该设计师的所有产品。

我无法完成这项工作。我可以获得所有设计师的列表,但是我似乎无法按商店类别对其进行过滤。这是我目前正在使用的相关代码 - 有没有更好的方法?任何帮助表示赞赏。

//load the current category
$store_category = Mage::app()->getStore()->getRootCategoryId();

//get all product designers
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
   ->setEntityTypeFilter($product->getResource()->getTypeId())
   ->addFieldToFilter('attribute_code', 'designer') // This can be changed to any attribute code
   ->load(false);
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$designers = $attribute->getSource()->getAllOptions(false);

//get all products
$collection = Mage::getModel('catalog/product')->getCollection();
$new_collection = Mage::getModel('catalog/category')->load($store_category)->getProductCollection();


//filter to only get visible products
$collection->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);

//filter by category - not working correctly
$collection->addCategoryFilter(Mage::getModel('catalog/category')->load($store_category));

//get products in stock
$collection->joinField('stock_status','cataloginventory/stock_status','stock_status',
      'product_id=entity_id', array(
      'stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK
   ));

//get all products with the designer attribute
$collection->addAttributeToSelect('designer');

//count the number of designers
$collection->addExpressionAttributeToSelect('designers_count', 'COUNT({{attribute}})', 'designer');

//group by the designer - limits the collection by products that have a designer setup
$collection->groupByAttribute('designer');
echo 'collection count->'.$collection->count();
//loop through collection and add the number of designers and designer id to an array
foreach($collection as $item)
{
   //get the designer id and the designers count
   $designer_id = $item->getDesigner();
   $designers_count = $item->getData('designers_count');

   //skip if the designers count is 0
   if($designers_count == 0)
   {
      continue;
   }// if

   //skip if the designer_id is empty
   if(empty($designer_id))
   {
      continue;
   }// if

   //add information to array
   $designers_in_use[$designer_id] = $designers_count;
}// foreach

【问题讨论】:

    标签: templates list magento attributes filter


    【解决方案1】:

    Magento 使用索引表来生成分层导航: 在 magento 1.3 版中,您有 catalogindex_eav 表(store_id,entity_id,attribute_id,value) 在 magento 1.4 版中,您有几个表,但我认为您只需要 catalog_product_index_eav (entity_id, attribute_id, store_id,value)

    对于 magento 1.4,看看 Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute 类 - getCount 方法:

    基本思想是在产品集合和catalog_product_index_eav表之间进行连接:

    // excerpt from Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute getCount()
    $conditions = array(
                "{$tableAlias}.entity_id = e.entity_id",
                $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
                $connection->quoteInto("{$tableAlias}.store_id = ?", $filter->getStoreId()),
            );
    
            $select
                ->join(
                    array($tableAlias => $this->getMainTable()),
                    join(' AND ', $conditions),
                    array('value', 'count' => "COUNT({$tableAlias}.entity_id)"))
                ->group("{$tableAlias}.value");  
    

    【讨论】:

    • 为此 +1 - 您的回答激发了我深入研究 Magento 正在做的事情。感谢您的宝贵时间。
    【解决方案2】:

    好的 - 所以我想出了如何做到这一点。首先,我在 app/design/frontend/my-layout/default/template 目录中创建了一个 Designers/view.phtml 目录和文件。我找到了我想要添加设计师的 CMS 页面,并在内容中调用了这个块: {{block type="catalog/layer_view" template="designers/view.phtml"}}

    从这里开始很简单。我编写了与此类似的代码并将其放置在上面的模板文件中 - 比以前的方式要容易得多。

    $filters = $this->getFilters();
    
    foreach($filters as $filter)
    {
       if($filter->getName()=='Designer')
       {
          echo '<ul>';
          foreach ($filter->getItems() as $_item)
          {
             echo '<li><a href="'.$filter->urlEscape($_item->getUrl()).'">'.$_item->getLabel().'</a></li>';
          }// foreach
          echo '</ul>';
    
          //stop foreach execution
          break;
       }//if
    }// foreach
    

    【讨论】:

      猜你喜欢
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      相关资源
      最近更新 更多