Add categories column to the product grid in Magento admin

后端 未结 5 1330
梦如初夏
梦如初夏 2021-02-02 00:19

I\'m trying to add a category column to the product grid. I\'ve modified Mage_Adminhtml_Block_Catalog_Product_Grid. Added the following to _prepareCollection<

5条回答
  •  攒了一身酷
    2021-02-02 00:51

    to display the name o all categories you can use

    $this->addColumn('category_ids',
            array(
                                        'header'=> Mage::helper('catalog')->__('Category'),
                                        'type'  => 'options',
                                        'index' => 'category_ids',
                                        'options' => $this->catOptions,
                                        'renderer'  => 'Your_Module_Block_Adminhtml_List_Cat',
    ));
    

    in your class block

     class Your_Module_Block_Adminhtml_List_Cat extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    {
        public function render(Varien_Object $row)
        {
            $product = Mage::getModel('catalog/product')->load($row->getEntityId());
            $cats = $product->getCategoryIds();
            $allCats = '';
            foreach($cats as $key => $cat)
            {
                $_category = Mage::getModel('catalog/category')->load($cat);
                $allCats.= $_category->getName();
                if($key < count($cats)-1)
                    $allCats.= ' ,';
            }
            return $allCats;
        }
    
    }
    

提交回复
热议问题