Creating custom reports in Magento allows you to gain deeper insights into your store’s performance, customer behavior, and sales trends. Magento provides built-in reporting tools, but for more specific needs, custom reports can be invaluable. Here’s a step-by-step guide on how to create custom reports in Magento:
Step 1: Understanding Magento’s Reporting Capabilities
Magento offers built-in reports under the Reports section, including sales, customers, products, and marketing reports. For custom reports, you can either use Magento’s built-in capabilities, third-party extensions, or build them programmatically.
Step 2: Using Third-Party Extensions
There are several extensions available that can simplify the process of creating custom reports. Here are a few popular ones:
- Aheadworks Advanced Reports
- URL: Aheadworks Advanced Reports
- Features: Customizable reports, data visualization, and advanced filtering.
- Mirasvit Advanced Reports
- URL: Mirasvit Advanced Reports
- Features: Multiple pre-built report templates, custom report builder, and data export options.
- Amasty Custom Reports
- URL: Amasty Custom Reports
- Features: User-friendly report builder, various chart types, and scheduling reports.
Step 3: Creating Custom Reports Programmatically
If you have specific reporting needs that cannot be met with third-party extensions, you can create custom reports programmatically. Here’s how to do it:
3.1 Set Up a Module
First, create a custom module to handle your custom reports.
Step 1: Create Module Directory Structure
plaintext
Copy code
app/code/YourCompany/CustomReports
├── etc
│ ├── adminhtml
│ │ └── routes.xml
│ └── module.xml
├── registration.php
└── Model
└── Report.php
Step 2: Define the Module
module.xml: app/code/YourCompany/CustomReports/etc/module.xml
xml
Copy code
<?xml version=”1.0″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:Module/etc/module.xsd”>
<module name=”YourCompany_CustomReports” setup_version=”1.0.0″/>
</config>
registration.php: app/code/YourCompany/CustomReports/registration.php
php
Copy code
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
‘YourCompany_CustomReports’,
__DIR__
);
Step 3: Define Admin Route
routes.xml: app/code/YourCompany/CustomReports/etc/adminhtml/routes.xml
xml
Copy code
<?xml version=”1.0″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:App/etc/routes.xsd”>
<router id=”admin”>
<route id=”customreports” frontName=”customreports”>
<module name=”YourCompany_CustomReports” />
</route>
</router>
</config>
3.2 Create the Report Model
Step 1: Create Report Model
Report.php: app/code/YourCompany/CustomReports/Model/Report.php
php
Copy code
<?php
namespace YourCompany\CustomReports\Model;
use Magento\Framework\Model\AbstractModel;
class Report extends AbstractModel
{
protected function _construct()
{
$this->_init(‘YourCompany\CustomReports\Model\ResourceModel\Report’);
}
public function getCustomReportData()
{
$connection = $this->getResource()->getConnection();
$select = $connection->select()
->from(
[‘sales_order’ => $this->getResource()->getTable(‘sales_order’)],
[‘entity_id’, ‘increment_id’, ‘created_at’, ‘base_grand_total’]
)
->where(‘status = ?’, ‘complete’);
return $connection->fetchAll($select);
}
}
Step 2: Create Resource Model
ResourceModel/Report.php: app/code/YourCompany/CustomReports/Model/ResourceModel/Report.php
php
Copy code
<?php
namespace YourCompany\CustomReports\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Report extends AbstractDb
{
protected function _construct()
{
$this->_init(‘sales_order’, ‘entity_id’);
}
}
3.3 Create Admin Controller
Step 1: Create Controller
Controller/Adminhtml/Report/Index.php: app/code/YourCompany/CustomReports/Controller/Adminhtml/Report/Index.php
php
Copy code
<?php
namespace YourCompany\CustomReports\Controller\Adminhtml\Report;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use YourCompany\CustomReports\Model\Report;
class Index extends Action
{
protected $resultPageFactory;
protected $report;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
Report $report
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->report = $report;
}
public function execute()
{
$reportData = $this->report->getCustomReportData();
foreach ($reportData as $data) {
// Process or display the data as needed
var_dump($data);
}
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu(‘YourCompany_CustomReports::report’);
$resultPage->getConfig()->getTitle()->prepend(__(‘Custom Reports’));
return $resultPage;
}
}
3.4 Create Admin Menu
Step 1: Create menu.xml
menu.xml: app/code/YourCompany/CustomReports/etc/adminhtml/menu.xml
xml
Copy code
<?xml version=”1.0″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:Menu/etc/menu.xsd”>
<menu>
<add id=”YourCompany_CustomReports::report”
title=”Custom Reports”
module=”YourCompany_CustomReports”
sortOrder=”10″
parent=”Magento_Backend::content”
action=”customreports/report/index”
resource=”YourCompany_CustomReports::report” />
</menu>
</config>
Step 4: Verify and Test the Report
Run Setup Upgrade: Ensure the module is registered and configured properly.
bash
Copy code
php bin/magento setup:upgrade
php bin/magento cache:clean
- Access the Report: Navigate to the Magento Admin panel, and you should see the “Custom Reports” menu item under “Content.” Click on it to view your custom report data.
Step 5: Extend and Customize
- Add Filters and Parameters: Enhance your report by adding filters and parameters to the admin interface.
- Export Options: Implement options to export report data in formats like CSV or Excel.
- Data Visualization: Integrate charts and graphs for better data visualization.
Conclusion
Creating custom reports in Magento can provide valuable insights into your store’s performance. Whether you use third-party extensions or develop reports programmatically, you can tailor the reporting to meet your specific needs.
If you need further assistance or specific recommendations on creating custom reports in Magento, feel free to ask!
Ready to take your e-commerce business to the next level? We’re here to help you succeed in the digital marketplace. Whether you’re looking to launch a new online store or optimize an existing one, our team at 247Commerce has the expertise and solutions to meet your needs.
Email: hey@247commerce.co.uk
Phone: +44 20 4547 9292