Magento:将可过滤属性列表添加到模板文件

发布于 2024-09-25 08:18:47 字数 2706 浏览 2 评论 0原文

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

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

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

//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

I have a Men and Women store setup under the same website in Magento. The Men website runs off of the Men category and the Women site off of the Women category. I have a filterable custom attribute setup entitled 'designer'. Currently, the layered navigation on a category page shows the Designer attribute for the products in that store along with any designers that are related to a product. I would like to show this list on another page.

I would like to take the Designer list that shows up in the layered navigation and put it into a template file. The idea is that a user will come to my site and want to see a list of all the designers I have in stock depending on which store they are viewing. For the Men store they would see a list of all men designers in stock and the same for the Women store. From this designer page they can choose there favorite designer and shop all products for that designer.

I'm having trouble getting this accomplished. I can get a list of all the designers, however I cannot seem to filter it by the store category. Here is the relevant code I am using currently - is there a better way of doing this? Any help is appreciated.

//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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2024-10-02 08:18:47

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");  

Magento uses index tables to generate the layered navigation:
in magento version 1.3 you have the catalogindex_eav table (store_id,entity_id, attribute_id,value)
in magento version 1.4 you have several tables but I think you only need catalog_product_index_eav (entity_id, attribute_id, store_id,value)

For magento 1.4 take a look at the Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute class - getCount method:

the basic idea is to make a join between the product collection and the catalog_product_index_eav table:

// 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");  
丢了幸福的猪 2024-10-02 08:18:47

好的 - 所以我想出了如何做到这一点。首先,我在 app/design/frontend/my-layout/default/template 目录中创建了 designs/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

OK - So I figured out how to do this. First, I created a designers/view.phtml directory and file within the app/design/frontend/my-layout/default/template directory. The I found the CMS page I wanted to add the Designers to and called this block in the content:
{{block type="catalog/layer_view" template="designers/view.phtml"}}

From here it was pretty simple. I programmed code similar to this and placed it in the above template file - so much easier then doing it the way I was before.

$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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文