【问题标题】:How to get all manufacturers for category in Magento如何在 Magento 中获取所有类别的制造商
【发布时间】:2011-05-16 12:55:13
【问题描述】:

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

首先我在重新定义的类别模型/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

感谢您的帮助!

【问题讨论】:

  • 你必须得到所有的id(已经有这个)并查询属性产品关系
  • 我需要用对象 Mage_Catalog_Model_Resource_Eav_Attribute 来做吗?

标签: magento attributes categories magento-1.4


【解决方案1】:

您应该如何获取某个类别的所有制造商:

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

希望这很有用。
干杯!

【讨论】:

    【解决方案2】:

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

    我的建议是为给定类别收集产品产品集合,例如:

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

    然后遍历它并获取给定类别的所有属性值。 缓存结果。 在 magento 中也是如此(当然是以“magento 方式”)

    【讨论】:

      【解决方案3】:

      花了很长时间,但我的回答可能会对某人有所帮助。

      如果您需要在类别页面上获取类别过滤器,您可能会得到错误的结果。

      我的代码基于代码@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');
      

      【讨论】:

        【解决方案4】:

        如果您想在制造商页面中添加分层导航。请将制造商添加为类别,并使用以下 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();
        }
        

        【讨论】:

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