0

I am showing products on grid.Now i am facing problem .I want to group the the same title products. My code is :

protected function _prepareCollection()
  {
    $collection = Mage::getModel('catalog/product')->getCollection()
                  ->addAttributeToSelect('*');
        $this->setCollection($collection);
      return parent::_prepareCollection();
  }

  protected function _prepareColumns()
  {

      $this->addColumn('name', array(
          'header'    => Mage::helper('module')->__('Product Name'),
          'align'     =>'left',
          'index'     => 'name'
      ));

      $this->addColumn('entity_id', array(
          'header'    => Mage::helper('catalog')->__('Products Ids'),
          'align'     =>'left',
          'width'     => '200px',
          'index'     => 'entity_id',
      ));

        $this->addColumn('action',
            array(
                'header'    =>  Mage::helper('module')->__('Action'),
                'width'     => '100',
                'type'      => 'action',
                'getter'    => 'getId',
                'actions'   => array(
                    array(
                        'caption'   => Mage::helper('module')->__('View Details'),
                        'url'       => array('base'=> '*/*/view'),
                        'field'     => 'id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));

      return parent::_prepareColumns();
  }

I want to to skip product name if it matches but show his id on Products ids with some like (1,2,3) etc.

2

In my example I added an additional column called entity_id_grouped both in the SELECT statement as well as the grid as you may not want do mess around with the original entity_id column in some cases.

First, add the column to your grid in _prepareColumns():

$this->addColumn('entity_id_grouped',
    array(
        'header'=> Mage::helper('catalog')->__('Product Ids'),
        'align' =>'left',
        'width' => '200px',
        'index' => 'entity_id_grouped',
        'type'  => 'text',
        'filter_condition_callback' => array($this, '_groupedEntityIdsFilter')
));

Note that I changed the column type to text. The filter_condition_callback parameter defines a custom method that is called when you filter for this column.

Next, group the results by name and use the built-in method to concatenate the product IDs in _prepareCollection():

$collection->groupByAttribute('name');
Mage::getResourceHelper('core')->addGroupConcatColumn(
    $collection->getSelect(),
    'entity_id_grouped',
    '`e`.entity_id'
);

Finally, add the mentioned method _groupedEntityIdsFilter() to be able to filter by product ids:

protected function _groupedEntityIdsFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $this->getCollection()->getSelect()->having(
        "GROUP_CONCAT(`e`.entity_id SEPARATOR ',') LIKE ?"
        , "%$value%");


    return $this;
}

Before the modification:

Grid before the modification

After the modification:

Grid after the modification

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.