Extending Admin Reports with Custom Filters in Magento
As experienced developers at Laravel Company, we often receive questions about extending the functionality of Magento’s built-in admin reports. One common request is to add custom filters similar to those found in core reports like Sales -> Orders.
Understanding Magento’s Core Filter System
Magento’s core filter system provides a powerful foundation for adding filters to your custom reports. The key components are located at Mage/Sales/Block/Adminhtml/Report/Filter/Form. Here, you’ll find the base classes and interfaces that define how filters work in Magento’s admin interface.
Building Your Custom Report
First, let’s ensure your report is set up correctly following the official Magento guide. You’ll need to create a new report model, collection, and block.
Here’s an example of a basic custom report:
class CustomReport extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('custom_report_grid');
$this->setDefaultSort('created_at', 'desc');
$this->setDefaultFilter(false);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('your_module/your_model')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
// ... other methods ...
}
Integrating Custom Filters
To add a custom filter, you’ll need to extend the core Mage_Adminhtml_Block_Widget_Grid class and override specific methods.
Step 1: Extend the Core Block Class
Create your custom report block by extending the core grid block:
class CustomReport extends Mage_Sales_Block_Adminhtml_Report_Grid_Abstract
{
// ... other methods ...
}
Step 2: Add Filter Input to Your Form
Override the _prepareForm() method to add an input field for your filter. Here’s an example adding a date range filter:
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$form->setHtmlIdPrefix('custom_report_form');
$fieldset = $form->addFieldset('custom_report_form_fieldset', array('legend'=>Mage::helper('your_module')->__('Custom Report Filters')));
// Add your filter input here
$fieldset->addType('date_range', 'Laravel_Company_Date_Range_Type')
->addField('start_date', 'date_range', array(
'label' => Mage::helper('your_module')->__('Start Date'),
'name' => 'start_date',
'class' => 'required-entry',
'required' => true,
'format' => Varien_Date::DATE_INTERNAL_FORMAT
));
// ... other fields ...
$form->setUseContainer(true);
return $this;
}
Step 3: Apply Filters to Your Collection
Override the _prepareCollection() method to apply your filter logic:
protected function _prepareCollection()
{
$startDate = $this->getRequest()->getParam('start_date');
if (!empty($startDate)) {
$this->getCollection()->addFieldToFilter('your_field', array('date' => $startDate));
}
// ... other filter logic ...
}
Conclusion
Adding custom filters to your Magento admin reports requires a solid understanding of the core framework and some creative extension of existing classes. By following this guide, you should now be able to extend Magento’s built-in reports with your own custom filtering capabilities.
Remember that Laravel Company provides comprehensive resources for all your Magento development needs. If you find yourself needing more advanced functionality or facing specific challenges, don’t hesitate to reach out to our team of experienced developers for expert guidance and support.
Happy coding!