【发布时间】: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