2

I would like to remove some of the default columns from the product grid in the Magento admin. I have tapped into the core_block_abstract_prepare_layout_before event an can successfully add new columns using addColumnAfter(), but when I try a removeColumn() on a default column it doesn't seem to do anything.

Here is my observer method...

public function updateColumns($observer)
{

    $block = $observer->getBlock();
    if (!isset($block)) return $this;

    if ($block->getType() == 'adminhtml/catalog_product_grid') {

        $boolOptions = array('1' => 'Yes', '0' => 'No');

        // Add new columns
        $block->addColumnAfter('model_number', array(
            'header' => 'Model No.',
            'type' => 'text',
            'index' => 'model_number',
        ), 'sku');
        $block->addColumnAfter('discontinued', array(
            'header' => 'Disc.',
            'type' => 'options',
            'index' => 'discontinued',
            'options' => $boolOptions,
        ), 'qty');

        // Remove columns
        $block->removeColumn('status');

    }

}

1 Answer 1

3

I ended up resolving this by changing the event I was observing to adminhtml_block_html_before and then adding a little fancy finagling to re-sort the columns and allow for proper filtering. The old event was occuring before the default columns were being set up which is why they couldn't be removed.

Here is the updated method with the new lines added below $block->removeColumn('status');

public function updateColumns($observer)
{

    $block = $observer->getBlock();
    if (!isset($block)) return $this;

    if ($block->getType() == 'adminhtml/catalog_product_grid') {

        $boolOptions = array('1' => 'Yes', '0' => 'No');

        // Add new columns
        $block->addColumnAfter('model_number', array(
            'header' => 'Model No.',
            'type' => 'text',
            'index' => 'model_number',
        ), 'sku');
        $block->addColumnAfter('discontinued', array(
            'header' => 'Disc.',
            'type' => 'options',
            'index' => 'discontinued',
            'options' => $boolOptions,
        ), 'qty');

        // Remove columns
        $block->removeColumn('status');

        $block->sortColumnsByOrder();

        // register current block, needed to extend the collection in the observer
        Mage::register('jks_grid_current_block', $block);
        // call _prepareCollection to reload the collection and apply column filters
        $this->_callProtectedMethod($block, '_prepareCollection');
        // remove current block to prevent race conditions in later collection loads
        Mage::unregister('jks_grid_current_block');

    }

}

Thanks goes out to this nifty GridControl extension which helped make the solution more obvious.

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.