Here I am giving my first steps in magento.
And I came across a nescessidade
in: Reports-> Products-> Products ordered
Has the following filters:
Would add a new filter, such as that in: Reports-> Sales-> Orders
Is that possible?
If yes, could someone give me some help? I did a search for extension or someone with similar problems but not getting suceso, I'll be grateful for any guidance.
Thank you for your attention.
Considering that you said these are your first steps in Magento this might be tricky, bit I try to answer it.
You'll need to know how to create your own module. I suggest following a tutorial, there are lot's of them out there. For example: http://www.smashingmagazine.com/2012/03/01/basics-creating-magento-module/
Then you can start changing the products ordered report in your extension. I've just done this, so it might not be optimal or 100% bug-free, but it should give you an idea. Please look for yournamespace
and yourmodule
(case-sensitive) in below code and replace it with your namespace and module name.
First, in app/design/adminhtml/default/default/layout/yourmodule.xml
we define the blocks needed for the filter and report grid:
<layout>
<adminhtml_report_product_sold>
<reference name="content">
<block type="adminhtml/report_product_sold" template="report/grid/container.phtml" name="product.report.grid.container">
<block type="adminhtml/store_switcher" template="report/store/switcher/enhanced.phtml" name="store.switcher">
<action method="setStoreVarName"><var_name>store_ids</var_name></action>
</block>
<block type="yourmodule/adminhtml_report_filter_form" name="grid.filter.form">
<action method="setFieldVisibility">
<field>report_type</field>
<visibility>0</visibility>
</action>
<action method="setFieldVisibility">
<field>show_empty_rows</field>
<visibility>0</visibility>
</action>
</block>
</block>
</reference>
</adminhtml_report_product_sold>
</layout>
And we define the new block in app/code/local/Yournamespace/Yourmodule/Block/Adminhtml/Report/Product/Sold.php
:
class Yournamespace_Yourmodule_Block_Adminhtml_Report_Product_Sold extends Mage_Adminhtml_Block_Report_Product_Sold
{
/**
* Override the default products sold report constructor
*/
public function __construct()
{
parent::__construct();
$this->_controller = 'report_product_sold';
$this->_headerText = Mage::helper('reports')->__('Products Ordered');
$this->setTemplate('report/grid/container.phtml');
$this->_removeButton('add');
$this->addButton('filter_form_submit', array(
'label' => Mage::helper('reports')->__('Show Report'),
'onclick' => 'filterFormSubmit()'
));
}
/**
* Get filter url
*
* @return string
*/
public function getFilterUrl()
{
$this->getRequest()->setParam('filter', null);
return $this->getUrl('*/*/sold', array('_current' => true));
}
}
Then, we need a custom controller for the product sold action, overriding the default one. In app/code/local/Yournamespace/Yourmodule/controllers/Adminhtml/Report/ProductController.php
:
require_once 'Mage/Adminhtml/controllers/Report/ProductController.php';
class Yournamespace_Yourmodule_Adminhtml_Report_ProductController extends Mage_Adminhtml_Report_ProductController
{
public function soldAction()
{
$this->_title($this->__('Reports'))->_title($this->__('Products'))->_title($this->__('Products Ordered'));
$this->_initAction()
->_setActiveMenu('report/products/sold')
->_addBreadcrumb(Mage::helper('reports')->__('Products Ordered'), Mage::helper('reports')->__('Products Ordered'));
$gridBlock = $this->getLayout()->getBlock('report_product_sold.grid');
$filterFormBlock = $this->getLayout()->getBlock('grid.filter.form');
$this->_initReportAction(array(
$gridBlock,
$filterFormBlock
));
$this->renderLayout();
}
}
Then in app/code/local/Yournamespace/Yourmodule/Block/Adminhtml/Report/Filter/Form.php
add your custom filter field: (I added category filter as an example.)
class Yournamespace_Yourmodule_Block_Adminhtml_Report_Filter_Form extends Mage_Adminhtml_Block_Report_Filter_Form
{
protected function _prepareForm()
{
parent::_prepareForm();
$fieldset = $this->getForm()->getElement('base_fieldset');
$fieldset->addField('product_categories', 'select', array(
'name' => 'product_categories',
'options' => array('0' => '', '1' => 'Category 1', '2' => 'Category 2'),
'label' => Mage::helper('reports')->__('Product Categories'),
'title' => Mage::helper('reports')->__('Product Categories')
));
return $this;
}
}
Then I create the grid in app/code/local/Yournamespace/Yourmodule/Block/Adminhtml/Report/Product/Sold/Grid.php
: (Explanation below...)
class Yournamespace_Yourmodule_Block_Adminhtml_Report_Product_Sold_Grid extends Mage_Adminhtml_Block_Report_Product_Sold_Grid
{
protected function _construct()
{
parent::_construct();
$this->setDateFilterVisibility(false);
}
protected function _prepareCollection()
{
parent::_prepareCollection();
$collection = $this->getCollection();
$collection->initReport('yournamespace_yourmodule/report_product_sold_collection');
$collection->setPeriod($this->getFilter('period_type'));
if ($this->getFilter('from') && $this->getFilter('to')) {
/**
* Validate from and to date
*/
try {
$from = $this->getLocale()->date($this->getFilter('from'), Zend_Date::DATE_SHORT, null, false);
$to = $this->getLocale()->date($this->getFilter('to'), Zend_Date::DATE_SHORT, null, false);
$collection->setInterval($from, $to);
}
catch (Exception $e) {
$this->_errors[] = Mage::helper('reports')->__('Invalid date specified.');
}
}
$collection->setCategoryFilter($this->getFilter('product_categories'));
return $this;
}
protected function _prepareColumns()
{
$this->addColumnAfter(
'product_categories',
array(
'header' => Mage::helper('yourmodule')->__('Product Categories'),
'index' => 'product_categories',
'filter' => false,
'sortable' => false,
),
'name'
);
return parent::_prepareColumns();
}
}
Things to note here:
$this->setDateFilterVisibility(false)
removes the default filter.
$collection->initReport('yournamespace_yourmodule/report_product_sold_collection');
initializes the grid with our collection (will be defined further down).
$collection->setCategoryFilter($this->getFilter('product_categories'));
will set the filter on our collection. This needs to be defined as this is not a default filter, unlike the from
and to
filters.
- In
_prepareColumns()
I add the product category column to the grid.
Then we need the following classes to pass our custom filter value from the grid to the report collection, then to the report and eventually to the products sold collection:
In app/code/local/Yournamespace/Yourmodule/Model/Mysql4/Report/Collection.php
:
class Yournamespace_Yourmodule_Model_Mysql4_Report_Collection extends Mage_Reports_Model_Mysql4_Report_Collection
{
public function setCategoryFilter($categoryId)
{
$this->_model->setCategoryFilter($categoryId);
}
}
In app/code/local/Yournamespace/Yourmodule/Model/Report.php
:
class Yournamespace_Yourmodule_Model_Report extends Mage_Reports_Model_Report
{
public function setCategoryFilter($categoryId)
{
$this->_reportModel->setCategoryFilter($categoryId);
}
}
In app/code/local/Yournamespace/Yourmodule/Model/Mysql4/Report/Product/Sold/Collection.php
:
class Yournamespace_Yourmodule_Model_Mysql4_Report_Product_Sold_Collection extends Mage_Reports_Model_Resource_Product_Sold_Collection
{
protected $_categoryFilter = null;
public function _prepareSelect(Varien_Db_Select $select)
{
parent::_prepareSelect($select);
//Build your (default) collection here...
if ($this->_categoryFilter) {
//Add custom conditions to the query
}
return $this->getSelect();
}
public function setCategoryFilter($categoryId)
{
$this->_categoryFilter = $categoryId;
}
}
I ripped off some code from above. Replace it with your own. $this->getSelect()
will give you the select. (Open the Varien_Db_Select
class if you need more info on this.)
Finally, to get this all working (hopefully) we have to have our rewrites (overrides) defined in app/code/local/Yournamespace/Yourmodule/etc/config.xml
: (You should have this file already when you created your module, I just post the new parts. All these should go between <config>
and </config>
.)
To make use of our controller:
<admin>
<routers>
<adminhtml>
<args>
<modules>
<Yournamespace_Yourmodule before="Mage_Adminhtml">Yournamespace_Yourmodule_Adminhtml</Yournamespace_Yourmodule>
</modules>
</args>
</adminhtml>
</routers>
</admin>
For the report and report collection overrides:
<global>
<models>
<reports>
<rewrite>
<report>Yournamespace_Yourmodule_Model_Report</report>
</rewrite>
</reports>
<reports_resource>
<rewrite>
<report_collection>Yournamespace_Yourmodule_Model_Mysql4_Report_Collection</report_collection>
</rewrite>
</reports_resource>
</models>
</global>
And for the new blocks:
<global>
<blocks>
<adminhtml>
<rewrite>
<report_product_sold>Yournamespace_Yourmodule_Block_Adminhtml_Report_Product_Sold</report_product_sold>
<report_product_sold_grid>Yournamespace_Yourmodule_Block_Adminhtml_Report_Product_Sold_Grid</report_product_sold_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
Hope this will work for you.