更新时间:2023-11-30 10:05:52
考虑到您说这是您在Magento中的第一步,这可能很棘手,我尝试回答一下.
Considering that you said these are your first steps in Magento this might be tricky, bit I try to answer it.
您将需要知道如何创建自己的模块.我建议遵循一个教程,其中有很多.例如: http://www.smashingmagazine.com/2012 /03/01/basics-creating-magento-module/
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/
然后,您可以开始在扩展程序中更改产品订购报告.我刚刚完成了此操作,因此它可能不是***选择,也不是100%无缺陷的,但是它应该可以为您提供一个思路.请在下面的代码中查找yournamespace
和yourmodule
(区分大小写),并将其替换为您的命名空间和模块名称.
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>
然后我们在app/code/local/Yournamespace/Yourmodule/Block/Adminhtml/Report/Product/Sold.php
中定义新块:
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));
}
}
然后,我们需要一个针对自售产品的自定义控制器,以覆盖默认控制器.在app/code/local/Yournamespace/Yourmodule/controllers/Adminhtml/Report/ProductController.php
:
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();
}
}
然后在app/code/local/Yournamespace/Yourmodule/Block/Adminhtml/Report/Filter/Form.php
中添加您的自定义过滤器字段:(我添加了类别过滤器作为示例.)
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();
}
}
这里要注意的事情:
$this->setDateFilterVisibility(false)
删除默认过滤器.$collection->setCategoryFilter($this->getFilter('product_categories'));
将在我们的收藏夹上设置过滤器.需要定义它,因为它不是默认过滤器,与from
和to
过滤器不同._prepareColumns()
中,我将产品类别列添加到网格中.$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._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:
在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
类.)
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.)
最后,要使所有这些正常运行(希望),我们必须在app/code/local/Yournamespace/Yourmodule/etc/config.xml
中定义我们的重写(覆盖):(创建模块时,您应该已经拥有此文件,我只是张贴了新零件.所有这些应该在<config>
和</config>
之间.)
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>
对于新块:
<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>
希望这对您有用.