2011-05-16 114 views
1

我在Magento CMS中遇到过这样的问题。我需要检索所有类别的制造商。 乍一看这不是问题,因为有一个过滤器块图层导航从中你可以采取必要的方法。如何获得Magento中的所有制造商类别

首先我创建重新定义的分类模型/app/code/local/Mage/ Catalog/Model/Category.php

public function getManufacturers() 
{ 
     $collection = Mage::getResourceModel('catalog/product_attribute_collection') 
      ->setItemObjectClass('catalog/resource_eav_attribute'); 

     $setIds = $this->getProductCollection()->getSetIds(); 

     $collection->getSelect()->distinct(true); 
     $collection 
      ->setAttributeSetFilter($setIds) 
      ->addStoreLabel(Mage::app()->getStore()->getId()) 
      ->setOrder('position', 'ASC'); 
     $collection->addIsFilterableFilter();; 
     $collection->load(); 

     return $collection; 
} 

的公共方法我在类模板调用这个方法:

$manufscturers = $_category->getManufacturers(); 

这样我们就得到了巨大的物体Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Attribute_Collection

然后:

$items = $manufscturers->getItems(); 

而我们得到的对象Mage_Catalog_Model_Resource_Eav_Attribute

然后我不知道该怎么做。这是一个死路一条。也许这是错误的方式?

Magento的版本 - 1.4.0.1

感谢您的帮助!

+0

你必须让所有的ID(已经有此)和查询属性的产品关系 – 2011-05-16 13:50:38

+0

我需要的对象Mage_Catalog_Model_Resource_Eav_Attribute办呢? – 2011-05-16 14:01:55

回答

2

这里是你应该如何得到所有厂商类别:

$category   = Mage::registry('current_category'); 
$layer    = Mage::getSingleton('catalog/layer'); 
$layer->setCurrentCategory($category); 
$attributes = $layer->getFilterableAttributes(); 
$manufacturers = array(); 
foreach ($attributes as $attribute) { 
    if ($attribute->getAttributeCode() == 'manufacturer') { 
     $filterBlockName = 'catalog/layer_filter_attribute'; 
     $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init(); 
     foreach($result->getItems() as $option) { 
      $manufacturers[$option->getValue()] = $option->getLabel(); 
     } 
    } 
} 
var_dump($manufacturers); 

希望这是有益的。
干杯!

1

据我所见,您已经实现了产品属性集合, 不依赖于给定的类别或产品集合。

我劝你是拿产品的产品收集等给出cateogory:

$layer = $this->getLayer(); 
$productCollection = $layer->getProductCollection(); 

然后遍历它,并得到所有所有的属性对于给定的类别值。 缓存结果。 完全相同的在Magento的完成(在“Magento的方式” ofcourse)

1

花了很长时间,但我的答案可能会帮助某人。

如果您需要在类别页面上获得类别过滤器,您可能会得到错误的结果。如果你想添加分层导航制造商页面

我的代码是基于代码@MagePsycho

public function getCategoryAttributeFilter($category, $attributeCode) 
{ 
    /** @var Mage_Catalog_Model_Layer $layer */ 
    $layer = Mage::getModel('catalog/layer'); 
    $layer->setCurrentCategory($category); 
    $attributes = $layer->getFilterableAttributes(); 

    $request = Mage::app()->getRequest(); 
    Mage::app()->setRequest($newRequest = new Mage_Core_Controller_Request_Http()); 
    $newRequest->setParam($attributeCode, false); 

    $items = array(); 
    foreach ($attributes as $attribute) { 
     if ($attribute->getAttributeCode() == $attributeCode) { 
      $filterBlockName = 'catalog/layer_filter_attribute'; 
      /** @var Mage_Catalog_Block_Layer_Filter_Attribute $block */ 
      $block = Mage::app()->getLayout()->createBlock($filterBlockName); 
      $result = $block->setLayer($layer)->setAttributeModel($attribute)->init(); 
      foreach($result->getItems() as $option) { 
       $manufacturers[$option->getValue()] = $option->getLabel(); 
      } 
      //$items = $result->getItems(); 
      break; 
     } 
    } 
    Mage::app()->setRequest($request); 

    Zend_Debug::dump($manufacturers); 

    return $items; 
} 
$category = Mage::getModel('catalog/category')->load(/*category id here*/); 
$helper->getCategoryAttributeFilter($category, 'brand'); 
1

。请添加制造商作为类别,并使用以下magento脚本来创建类别并以编程方式分配产品。

http://www.pearlbells.co.uk/how-to-create-new-categories-and-assigned-products-to-category-programmatically-magento/

$attrLabel = 'manufacturer'; 
$attr = $product->getResource()->getAttribute($attrLabel); 
$manufacturer_id = $attr->getSource()->getOptionId($manufacturer); 
$newproducts = $product->getCollection()->addAttributeToFilter(array(array('attribute'=>'manufacturer', 'eq'=> $manufacturer_id))); 

对于指定产品

$newCategory = array($list[0] , $list[$key]); 
foreach($newproducts as $prod) 
{ 
$prod->setCategoryIds(
     array_merge($prod->getCategoryIds(), $newCategory) 
    ); 
$prod->save(); 
} 
相关问题