Magento Grid Block has useful function for advanced filtering of a collection. It is called ‘filter_condition_callback’, and it is used in declaring a column. It takes actual collection, and callback function as parameters.
Major elements:
1 2 3 |
'filter_condition_callback' => array($this, 'filter_special_price'), |
and:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public function filter_special_price($collection, $column) { if (!$value = $column->getFilter()->getValue()) { return $this; } $dateToday = Mage::app()->getLocale()->date()->toString('M/d/y'); $this->getCollection()->addAttributeToFilter('special_price', array('neq' => '')) ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $dateToday)) ->addAttributeToFilter('special_to_date', array( 'or'=> array( 0 => array('date' => true, 'from' => $dateToday), 1 => array('is' => new Zend_Db_Expr('null'))) ), 'left'); return $this; } |
All Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php /** * * category AD * @copyright Copyright (c) by Adrian Badowski * @user Adrian Badowski */ class AD_Catalog_Block_Adminhtml_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Catalog_Category_Tab_Product { protected function _prepareColumns() { if (!$this->getCategory()->getProductsReadonly()) { $this->addColumn('in_category', array( 'header_css_class' => 'a-center', 'type' => 'checkbox', 'name' => 'in_category', 'values' => $this->_getSelectedProducts(), 'align' => 'center', 'index' => 'entity_id' )); } $this->addColumn('entity_id', array( 'header' => Mage::helper('catalog')->__('ID'), 'sortable' => true, 'width' => '60', 'index' => 'entity_id' )); $this->addColumn('name', array( 'header' => Mage::helper('catalog')->__('Name'), 'index' => 'name' )); $this->addColumn('sku', array( 'header' => Mage::helper('catalog')->__('SKU'), 'width' => '80', 'index' => 'sku' )); $this->addColumn('price', array( 'header' => Mage::helper('catalog')->__('Price'), 'type' => 'currency', 'width' => '1', 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE), 'index' => 'price' )); $this->addColumn('has_special_price', array( 'header' => Mage::helper('catalog')->__('Special price'), 'width' => '1', 'type' => 'checkbox', 'index' => 'Has special price', 'filter_condition_callback' => array($this, 'filter_special_price'), )); $this->addColumn('position', array( 'header' => Mage::helper('catalog')->__('Position'), 'width' => '1', 'type' => 'number', 'index' => 'position', 'editable' => !$this->getCategory()->getProductsReadonly() //'renderer' => 'adminhtml/widget_grid_column_renderer_input' )); return parent::_prepareColumns(); } public function filter_special_price($collection, $column) { if (!$value = $column->getFilter()->getValue()) { return $this; } $dateToday = Mage::app()->getLocale()->date()->toString('M/d/y'); $this->getCollection()->addAttributeToFilter('special_price', array('neq' => '')) ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $dateToday)) ->addAttributeToFilter('special_to_date', array( 'or'=> array( 0 => array('date' => true, 'from' => $dateToday), 1 => array('is' => new Zend_Db_Expr('null'))) ), 'left'); return $this; } } |
Leave a reply