最多 Magento报告 建立论坛,博客,教程等似乎都专注于获取现有的Magento报告,要么 复制或扩展 它。其中大多数是链接到特定数据集的网格,并且所有网格都包含标准过滤器,即从/到日期和期间(加上一些报告中的额外过滤器)。

但是,似乎很少有关于如何使用自定义过滤器创建完全自定义报告的信息。

例如,客户希望有两个报告 自定义过滤器 仅报告了 两个简单的聚合指标。

有帮助吗?

解决方案

首先,您需要生成一个自定义模块,创建以下文件:

    /app/etc/modules/Mycompany_Mymodule.xml 
    /app/design/adminhtml/default/default/layout/mymodule.xml 
    /app/code/local/Mycompany/Mymodule/Block/adminhtml/Mymodule/Grid.php
    /app/code/local/Mycompany/Mymodule/Block/adminhtml/Mymodule.php 
    /app/code/local/Mycompany/Mymodule/Block/Mymodule.php 
    /app/code/local/Mycompany/Mymodule/controllers/Adminhtml/MymoduleController.php 
    /app/code/local/Mycompany/Mymodule/etc/config.xml 
    /app/code/local/Mycompany/Mymodule/Helper/Data.php 
    /app/code/local/Mycompany/Mymodule/Model/Mymodule.php

定义您的模块 /app/etc/modules/mycompany_mymodule.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_Mymodule>
            <active>true</active>
            <codePool>local</codePool>
        </Mycompany_Mymodule>
    </modules>
</config>

完成将更新管理员视图的布局文件(我总是先执行此操作,因为我不想忘记它)。 /app/design/adminhtml/default/default/layout/mymodule.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <mymodule_adminhtml_mymodule_index>
        <reference name="content">
            <block type="mymodule/adminhtml_mymodule" name="mymodule" />
        </reference>
    </mymodule_adminhtml_mymodule_index>
</layout>

使用此内容创建配置文件 /app/code/local/mycompany/mymodule/etc/config.xml:

<?xml version="1.0"?>
<!-- 
/**
 * @category   Mycompany
 * @package    Mycompany_Mymodule
 * @author     Damian Alberto Pastorini
 */
 -->
<config>
    <modules>
        <Mycompany_Mymodule>
            <version>0.1.0</version>
        </Mycompany_Mymodule>
    </modules>
    <admin>
        <routers>
            <mymodule>
                <use>admin</use>
                <args>
                    <module>Mycompany_Mymodule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
    </admin>
    <adminhtml>
        <menu>
            <report>
                <children>
                    <mymodule translate="title" module="mymodule">
                        <title>Mymodule Report</title>
                        <action>mymodule/adminhtml_mymodule</action>
                    </mymodule>
                </children>
            </report>
        </menu>
        <acl>
            <resources>
                <all>
                    <title>Allow Everything</title>
                </all>
                <admin>
                    <children>
                        <report>
                            <children>
                                <mymodule translate="title" module="mymodule">
                                    <title>Mymodule Report</title>
                                    <action>mymodule/adminhtml_mymodule</action>
                                </mymodule>
                            </children>
                        </report>
                    </children>
                </admin>
            </resources>
        </acl>
        <layout>
            <updates>
                <mymodule>
                    <file>mymodule.xml</file>
                </mymodule>
            </updates>
        </layout>
    </adminhtml>
    <global>
        <models>
            <mymodule>
                <class>Mycompany_Mymodule_Model</class>
                <resourceModel>mymodule</resourceModel>
            </mymodule>
        </models>
        <resources>
            <mymodule_setup>
                <setup>
                    <module>Mycompany_Mymodule</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </mymodule_setup>
            <mymodule_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </mymodule_write>
            <mymodule_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </mymodule_read>
        </resources>
        <blocks>
            <mymodule>
                <class>Mycompany_Mymodule_Block</class>
            </mymodule>
        </blocks>
        <helpers>
            <mymodule>
                <class>Mycompany_Mymodule_Helper</class>
            </mymodule>
        </helpers>
    </global>
</config>

在这里,我们定义控制器,菜单访问和权限,模型,块和助手。

创建网格并指定所有列 /App/code/local/mycompany/mymodule/block/adminhtml/mymodule/grid.php:

<?php
 class Mycompany_Mymodule_Block_Adminhtml_Mymodule_Grid extends Mage_Adminhtml_Block_Report_Grid {

public function __construct() {
    parent::__construct();
    $this->setId('mymoduleGrid');
    $this->setDefaultSort('created_at');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
    $this->setSubReportSize(false);
}

protected function _prepareCollection() {
    parent::_prepareCollection();
    $this->getCollection()->initReport('mymodule/mymodule');
    return $this;
}

protected function _prepareColumns() {
    $this->addColumn('ordered_qty', array(
        'header'    =>Mage::helper('reports')->__('Quantity Ordered'),
        'align'     =>'right',
        'index'     =>'ordered_qty',
        'total'     =>'sum',
        'type'      =>'number'
    ));
    $this->addColumn('item_id', array(
        'header' => Mage::helper('mymodule')->__('Item ID'),
        'align' => 'right',
        'index' => 'item_id',
        'type'  => 'number',
        'total' => 'sum',
    ));
    $this->addExportType('*/*/exportCsv', Mage::helper('mymodule')->__('CSV'));
    $this->addExportType('*/*/exportXml', Mage::helper('mymodule')->__('XML'));
    return parent::_prepareColumns();
}

public function getRowUrl($row) {
    return false;
}

public function getReport($from, $to) {
    if ($from == '') {
        $from = $this->getFilter('report_from');
    }
    if ($to == '') {
        $to = $this->getFilter('report_to');
    }
    $totalObj = Mage::getModel('reports/totals');
    $totals = $totalObj->countTotals($this, $from, $to);
    $this->setTotals($totals);
    $this->addGrandTotals($totals);
    return $this->getCollection()->getReport($from, $to);
}
}

该文件最清楚,但我给您一些有关特定行的提示:

//此行指示用于获取数据的模型。

$this->getCollection()->initReport('mymodule/mymodule'); // it's used to indicate that this field must be totalized at the end. 
'total' =>'sum', // this is executed when you click on the rows grid, in case you return false (like the example) nothing will happen when you click on the rows grid. 
public function getRowUrl($row) {

对于下一步,创建网格容器块 /App/code/local/mycompany/mymodule/block/adminhtml/mymodule.php:

<?php
 class Mycompany_Mymodule_Block_Adminhtml_Mymodule extends Mage_Adminhtml_Block_Widget_Grid_Container {

public function __construct() {
    $this->_controller = 'adminhtml_mymodule';
    $this->_blockGroup = 'mymodule';
    $this->_headerText = Mage::helper('mymodule')->__('Mymodule Report');
    parent::__construct();
    $this->_removeButton('add');
}
}

在这里,我们添加此行以删除添加按钮://必须始终在 parent::__construct(); 线。 $this->_removeButton('add');

创建块容器 /app/code/local/mycompany/mymodule/block/mymodule.php:

<?php
 class Mycompany_Mymodule_Block_Mymodule extends Mage_Core_Block_Template {

public function _prepareLayout() {
    return parent::_prepareLayout();
}

public function getMymodule() {
    if (!$this->hasData('mymodule')) {
        $this->setData('mymodule', Mage::registry('mymodule'));
    }
    return $this->getData('mymodule');
} 
}

创建控制器 /App/code/local/mycompany/mymodule/controllers/adminhtml/mymyodulecontroller.php:

<?php

 class Mycompany_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_Action {

protected function _initAction() {
    $this->loadLayout();
    return $this;
}

public function indexAction() {
    $this->_initAction()
            ->renderLayout();
}

public function exportCsvAction() {
    $fileName = 'mymodule.csv';
    $content = $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_grid')
                    ->getCsv();
    $this->_sendUploadResponse($fileName, $content);
}

public function exportXmlAction() {
    $fileName = 'mymodule.xml';
    $content = $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_grid')
                    ->getXml();
    $this->_sendUploadResponse($fileName, $content);
}

protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream') {
    $response = $this->getResponse();
    $response->setHeader('HTTP/1.1 200 OK', '');
    $response->setHeader('Pragma', 'public', true);
    $response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
    $response->setHeader('Content-Disposition', 'attachment; filename=' . $fileName);
    $response->setHeader('Last-Modified', date('r'));
    $response->setHeader('Accept-Ranges', 'bytes');
    $response->setHeader('Content-Length', strlen($content));
    $response->setHeader('Content-type', $contentType);
    $response->setBody($content);
    $response->sendResponse();
    die;
}
}

然后空手 /app/code/local/mycompany/mymodule/helper/data.php:

<?php
class Mycompany_Mymodule_Helper_Data extends Mage_Core_Helper_Abstract
{

}

最后我们创建的模型将带来数据 /app/code/local/mycompany/mymodule/model/mymodule.php:

 <?php
    class Mycompany_Mymodule_Model_Mymodule extends Mage_Reports_Model_Mysql4_Order_Collection
{
    function __construct() {
        parent::__construct();
        $this->setResourceModel('sales/order_item');
        $this->_init('sales/order_item','item_id');
   }

    public function setDateRange($from, $to) {
        $this->_reset();
        $this->getSelect()
             ->joinInner(array(
                 'i' => $this->getTable('sales/order_item')),
                 'i.order_id = main_table.entity_id'
                 )
             ->where('i.parent_item_id is null')
             ->where("i.created_at BETWEEN '".$from."' AND '".$to."'")
             ->where('main_table.state = \'complete\'')
             ->columns(array('ordered_qty' => 'count(distinct `main_table`.`entity_id`)'));
        // uncomment next line to get the query log:
        // Mage::log('SQL: '.$this->getSelect()->__toString());
        return $this;
    }

    public function setStoreIds($storeIds)
    {
        return $this;
    }

    }
    ?>

这是一种从Magento Core模型中获取数据的自定义模型,您可以在这里定义任何模型,或者如果您已经拥有自己的模型 DB/表 您可以从中获取报告数据。 //此行重置默认情况下的原始查询。 $this->_reset();

我尝试添加所有这些文件,但是一旦我单击报告的新菜单项,就会出现空白页。

其他提示

只需重命名文件夹 adminhtmlAdminhtml 按照以下路径:

app/code/local/mycompany/mymodule/block/adminhtml/mymodule.php

许可以下: CC-BY-SA归因
scroll top