0

I overrode class My_CategoryFilters_Block_Adminhtml_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Widget_Grid and added the column 'visibility' to _prepareCollection and _prepareColumns. Whenever I try to filter, the grid does not refresh.

I read Grid doesn't appear in custom admin module in Magento and Magento Grid Container Block not loading grid . Both suggested around creating an overloaded Controller. Can someone with a little more Magento experience shed some light on what I'm missing?

Thanks in advance.

EDIT: Removed 'renderer' and 'controllers' from code.

This is what the tree looks like:

CategoryFilters
├── Block
│   └── Adminhtml
│       └── Catalog
│           └── Category
│               └── Tab
│                   └── Product.php
├── Core
│   └── etc
│       └── config.xml
└── etc

And this is my code:

protected function _prepareCollection()
{
    if ($this->getCategory()->getId()) {
        $this->setDefaultFilter(array('in_category'=>1));
    }
    $store = $this->_getStore();
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('price')
        ->addAttributeToSelect('store_id')
        ->addStoreFilter($this->getRequest()->getParam('store'))
        ->joinField('position',
            'catalog/category_product',
            'position',
            'product_id=entity_id',
            'category_id='.(int) $this->getRequest()->getParam('id', 0),
            'left')
        ->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
        ;

    $this->setCollection($collection);

    $this->getCollection()->addWebsiteNamesToResult();

    if ($this->getCategory()->getProductsReadonly()) {
        $productIds = $this->_getSelectedProducts();
        if (empty($productIds)) {
            $productIds = 0;
        }
        $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
    }

    return parent::_prepareCollection();
}


protected function _prepareColumns()
{
    $this->addColumn('visibility', array(
        'header'   => Mage::helper('catalog')->__('Visibility'),
        'width'    => '100',
        'sortable' => false,
        'index'    => 'visibility',
        'type'     => 'options',
        'options'  => Mage::getModel('catalog/product_visibility')->getOptionArray(),
        #'renderer' => new Rogue_CategoryFilters_Block_Adminhtml_Renderer_Visibility()
    ));
}
Community
  • 1
  • 1
Nate H
  • 322
  • 1
  • 5
  • 20
  • Have you checked this link? http://stackoverflow.com/questions/8879130/magento-how-do-i-add-a-column-to-products-selection-in-admin-category-page – Kalpesh Jul 27 '12 at 19:57
  • @KalpeshMehta I have tried exactly what is in [http://stackoverflow.com/a/10735418/1416040]. The filtering does not work. – Nate H Jul 27 '12 at 20:53
  • @Nate H, but do you see the visibility column? – Kalpesh Jul 27 '12 at 20:55
  • @KalpeshMehta yes, also tried http://stackoverflow.com/questions/8879130/magento-how-do-i-add-a-column-to-products-selection-in-admin-category-page – Nate H Jul 27 '12 at 20:56
  • @KalpeshMehta Yes, the visibility column shows. The total even repopulates (Total 4 records found), the grid does not refresh. – Nate H Jul 27 '12 at 20:58

1 Answers1

0

The answer goes a little beyond my scope of understanding. I haven't dug in too deep, but I'm assuming it has to do w/ the way the parents handle my request.

Posting the updated snippets of code.

protected function _addColumnFilterToCollection($column)
{
    // Set custom filter for in category flag
    if ($column->getId() == 'in_category') {
        $productIds = $this->_getSelectedProducts();
        if (empty($productIds)) {
            $productIds = 0;
        }
        if ($column->getFilter()->getValue()) {
            $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
        }
        elseif(!empty($productIds)) {
            $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
        }
    }
    else if ($column->getId() == 'websites') {
        $this->getCollection()->joinField('websites',
            'catalog/product_website',
            'website_id',
            'product_id=entity_id',
            null,
            'left');
        return parent::_addColumnFilterToCollection($column);
    }
    else {
        parent::_addColumnFilterToCollection($column);
    }

    return $this;
}

protected function _prepareCollection()
{
    $store = $this->_getStore();
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('price')
        ->addAttributeToSelect('store_id')
        ->addStoreFilter($this->getRequest()->getParam('store'))
        ->joinField('position',
            'catalog/category_product',
            'position',
            'product_id=entity_id',
            'category_id='.(int) $this->getRequest()->getParam('id', 0),
            'left');

    if ($store->getId()) {
        $collection->addStoreFilter($store);
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
    }
    else {
        $collection->addAttributeToSelect('visibility');
    }

    $this->setCollection($collection);

    if ($this->getCategory()->getProductsReadonly()) {
        $productIds = $this->_getSelectedProducts();
        if (empty($productIds)) {
            $productIds = 0;
        }
        $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
    }

    parent::_prepareCollection();
    $this->getCollection()->addWebsiteNamesToResult();

    return $this;
}
Nate H
  • 322
  • 1
  • 5
  • 20