views:

360

answers:

2

Hi,

I'm trying to add a categories column to the product grid. I've modified Mage_Adminhtml_Block_Catalog_Product_Grid. Added the following to _prepareCollection:

->joinField('category_ids',
            'catalog/category_product_index',
            'category_id',
            'product_id=entity_id',
            null,
            'left')

which gives me an error: a:5:{i:0;s:72:"Item (Mage_Catalog_Model_Product) with the same id "16243" already exist"

In prepareColumns I'm adding:

$this->addColumn('category_ids',
        array(
            'header'=> Mage::helper('catalog')->__('Categories'),
            'index' => 'category_ids',
            'width' => '150px'
    ));

How can I fix my query so I won't get the error? Is it possible to show and filter by category names instead of ids?

a forum post show a similar code but I couldn't make it work with categories http://www.magentocommerce.com/boards/viewthread/44534/

static protected $COLUMN_ID_TRADE_REFERENCES = 'ref_text';

protected function _prepareCollection()
{
    $store = $this->_getStore();
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('attribute_set_id')
        ->addAttributeToSelect('type_id')
        ->addAttributeToSelect('ref_text')
        ->joinTable('productreferences/reference',
            'product_id=entity_id',
            array('ref_text'),
            null,
            'left')
        ->joinField('qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left')
        ->addStaticField('ref_text')
        ->addExpressionAttributeToSelect(self::$COLUMN_ID_TRADE_REFERENCES,
            'GROUP_CONCAT(ref_text SEPARATOR " ; ")',
            'ref_text')
        ->groupByAttribute('entity_id');

Thanks

A: 

if you just want to add the category (not the category path) and if the product has only one category, add this to the collection-setup:

$collection->joinAttribute('catname','catalog_category/name','category_ids',null,'left');
56ksurfer