【问题标题】:Add more filters in report the products ordered - Magento在报告订购的产品中添加更多过滤器 - Magento
【发布时间】:2014-04-08 19:30:38
【问题描述】:

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

我遇到了一个必需品

在:报告-> 产品-> 订购的产品

具有以下过滤器:



将添加一个新的过滤器,例如:报告-> 销售-> 订单



这可能吗? 如果是的话,有人可以给我一些帮助吗?我搜索了扩展程序或有类似问题但没有获得成功的人,我将不胜感激任何指导。

感谢您的关注。

【问题讨论】:

    标签: magento magento-1.6


    【解决方案1】:

    考虑到您说这些是您在 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-&gt;setDateFilterVisibility(false) 删除默认过滤器。
    • $collection-&gt;initReport('yournamespace_yourmodule/report_product_sold_collection'); 用我们的集合初始化网格(将在下面进一步定义)。
    • $collection-&gt;setCategoryFilter($this-&gt;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-&gt;getSelect() 会给你选择。 (如果您需要更多信息,请打开 Varien_Db_Select 课程。)

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

    使用我们的控制器:

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

    希望这对你有用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多