在订购产品的报告中添加更多过滤器 - Magento

时间:2014-04-08 19:30:39

标签: magento magento-1.6

在这里,我将迈出第一步。

我遇到了一个nescessidade

in:Reports->产品展示>订购产品

有以下过滤器:

enter image description here



会添加一个新过滤器,例如:Reports->销售 - >订单



enter image description here

这可能吗? 如果是的话,有人可以给我一些帮助吗?我搜索了扩展或有类似问题但没有得到suceso的人,我会感激任何指导。

感谢您的关注。

1 个答案:

答案 0 :(得分:4)

考虑到你说这些是你在Magento的第一步,这可能很棘手,我试着回答它。

您需要知道如何创建自己的模块。我建议遵循一个教程,其中有很多。例如:http://www.smashingmagazine.com/2012/03/01/basics-creating-magento-module/

然后,您可以开始更改扩展程序中的产品订购报告。我刚刚完成了这个,所以它可能不是最佳的或100%无错误,但它应该给你一个想法。请在下面的代码中查找yournamespaceyourmodule(区分大小写),并将其替换为您的命名空间和模块名称。

首先,在app/design/adminhtml/default/default/layout/yourmodule.xml中,我们定义了过滤器和报告网格所需的块:

<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>

我们在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));
    }
}

然后,我们需要一个自定义控制器用于产品销售操作,覆盖默认操作。在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();
    }
}

然后在app/code/local/Yournamespace/Yourmodule/Block/Adminhtml/Report/Filter/Form.php添加自定义过滤器字段:(我添加了类别过滤器作为示例。)

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;
    }
 }

然后我在app/code/local/Yournamespace/Yourmodule/Block/Adminhtml/Report/Product/Sold/Grid.php创建网格:(下面的说明......)

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();
    }

}

此处需要注意的事项:

  • $this->setDateFilterVisibility(false)删除默认过滤器。
  • $collection->initReport('yournamespace_yourmodule/report_product_sold_collection');使用我们的集合初始化网格(将进一步定义)。
  • $collection->setCategoryFilter($this->getFilter('product_categories'));会在我们的集合上设置过滤器。这需要定义,因为这不是默认过滤器,与fromto过滤器不同。
  • _prepareColumns()中,我将产品类别列添加到网格中。

然后我们需要以下类将我们的自定义过滤器值从网格传递到报告集合,然后传递到报告,最后传递到产品销售集合:

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);
    }
}

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);
    }
}

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;
    }
}

我从上面扯掉了一些代码。用你自己的替换它。 $this->getSelect()会给你选择。 (如果您需要更多信息,请打开Varien_Db_Select课程。)

最后,为了让这一切工作(希望如此),我们必须在app/code/local/Yournamespace/Yourmodule/etc/config.xml中定义我们的重写(覆盖):(在创建模块时,您应该已经有了这个文件,我只是发布了新的部分。所有这些都应该在<config></config>之间。)

使用我们的控制器:

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <Yournamespace_Yourmodule before="Mage_Adminhtml">Yournamespace_Yourmodule_Adminhtml</Yournamespace_Yourmodule>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

对于报告和报告集合覆盖:

<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>

对于新的块:

<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>

希望这对你有用。