Comment implémenter un rapport personnalisé dans l'administration de Magento 2
How to Implement a Custom Report in Magento 2 Admin
If you're running a Magento 2 store, you know how important it is to have access to the right data. Magento 2 comes with a variety of built-in rapports, but sometimes you need something more specific to your entreprise needs. That's where custom rapports come in. Dans cet article, nous'll walk you through the process of creating a custom rapport in Magento 2 Admin. Don't worry if you're new to this; we'll take it étape par étape.
Why Create a Custom Report?
Custom rapports allow you to analyze data that is specific to your entreprise. Whether you need to track sales by a specific category, monitor client behavior, or analyze product performance, custom rapports can provide the insights you need. By creating a custom rapport, you can tailor the data to your exact prérequis, making it easier to make informed entreprise decisions.
Step 1: Set Up Your Module
Premièrement, you'll need to create a new module for your custom rapport. If you're not familiar with creating modules in Magento 2, don't worry—it's straightforward. Voici comment you can do it:
app/code/YourVendor/YourModule/registration.php
In this fichier, you'll register your module with Magento:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'YourVendor_YourModule',
__DIR__
);
Ensuite, create the module.xml fichier:
app/code/YourVendor/YourModule/etc/module.xml
And add les éléments suivants contenu:
<?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="YourVendor_YourModule" setup_version="1.0.0"/>
</config>
Step 2: Create the Report Grid
Now that your module is set up, it's time to create the rapport grid. The grid will display the data in the Magento 2 Admin panel. To do this, you'll need to create a few fichiers:
app/code/YourVendor/YourModule/Block/Adminhtml/Report/YourReport.php
In this fichier, you'll define the grid:
<?php
namespace YourVendor\YourModule\Block\Adminhtml\Report;
class YourReport extends \Magento\Backend\Block\Widget\Grid\Container
{
protected fonction _construct()
{
$this->_blockGroup = 'YourVendor_YourModule';
$this->_contrôleur = 'adminhtml_rapport_yourrapport';
$this->_headerText = __('Your Custom Report');
parent::_construct();
$this->buttonList->remove('add');
}
}
Ensuite, create the grid fichier:
app/code/YourVendor/YourModule/Block/Adminhtml/Report/YourReport/Grid.php
And add les éléments suivants contenu:
<?php
namespace YourVendor\YourModule\Block\Adminhtml\Report\YourReport;
class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
protected $_collectionFactory;
public fonction __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Backend\Helper\Data $back-officeHelper,
\YourVendor\YourModule\Model\ResourceModel\YourReport\CollectionFactory $collectionFactory,
tableau $data = []
) {
$this->_collectionFactory = $collectionFactory;
parent::__construct($context, $back-officeHelper, $data);
}
protected fonction _prepareCollection()
{
$this->setCollection($this->_collectionFactory->create());
return parent::_prepareCollection();
}
protected fonction _prepareColumns()
{
$this->addColumn('entity_id', [
'header' => __('ID'),
'index' => 'entity_id',
]);
$this->addColumn('name', [
'header' => __('Name'),
'index' => 'name',
]);
return parent::_prepareColumns();
}
}
Step 3: Create the Collection
The collection est responsable de fetching the data that sera displayed in the grid. Create the collection fichier:
app/code/YourVendor/YourModule/Model/ResourceModel/YourReport/Collection.php
And add les éléments suivants contenu:
<?php
namespace YourVendor\YourModule\Model\ResourceModel\YourReport;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected fonction _construct()
{
$this->_init(
'YourVendor\YourModule\Model\YourReport',
'YourVendor\YourModule\Model\ResourceModel\YourReport'
);
}
}
Ensuite, create the collection factory:
app/code/YourVendor/YourModule/Model/ResourceModel/YourReport/CollectionFactory.php
And add les éléments suivants contenu:
<?php
namespace YourVendor\YourModule\Model\ResourceModel\YourReport;
class CollectionFactory
{
protected $_objetManager = null;
protected $_instanceName = null;
public fonction __construct(
\Magento\Framework\ObjectManagerInterface $objetManager,
$instanceName = '\\YourVendor\\YourModule\\Model\\ResourceModel\\YourReport\\Collection'
) {
$this->_objetManager = $objetManager;
$this->_instanceName = $instanceName;
}
public fonction create(tableau $data = [])
{
return $this->_objetManager->create($this->_instanceName, $data);
}
}
Step 4: Create the Layout and Menu
Now that your grid and collection are set up, you'll need to create the layout and menu for your custom rapport. Premièrement, create the fichier de layout:
app/code/YourVendor/YourModule/view/adminhtml/layout/yourmodule_rapport_yourrapport_index.xml
And add les éléments suivants contenu:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="contenu">
<classe de bloc="YourVendor\YourModule\Block\Adminhtml\Report\YourReport" name="yourmodule_rapport_yourrapport"/>
</referenceContainer>
</body>
</page>
Ensuite, create the menu fichier:
app/code/YourVendor/YourModule/etc/adminhtml/menu.xml
And add les éléments suivants contenu:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="YourVendor_YourModule::rapport" title="Custom Reports" module="YourVendor_YourModule" triOrder="10" resource="Magento_Backend::contenu"/>
<add id="YourVendor_YourModule::rapport_yourrapport" title="Your Custom Report" module="YourVendor_YourModule" triOrder="10" parent="YourVendor_YourModule::rapport" action="yourmodule/rapport_yourrapport" resource="Magento_Backend::contenu"/>
</menu>
</config>
Step 5: Create the Controller
Enfin, you'll need to create the contrôleur that will handle the request for your custom rapport. Create the contrôleur fichier:
app/code/YourVendor/YourModule/Controller/Adminhtml/Report/YourReport/Index.php
And add les éléments suivants contenu:
<?php
namespace YourVendor\YourModule\Controller\Adminhtml\Report\YourReport;
class Index extends \Magento\Backend\App\Action
{
protected $resultPageFactory;
public fonction __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public fonction execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('YourVendor_YourModule::rapport_yourrapport');
$resultPage->getConfig()->getTitle()->prepend(__('Your Custom Report'));
return $resultPage;
}
}
Step 6: Test Your Custom Report
Une fois you've completed all the étapes, it's time to test your custom rapport. Log in to your Magento 2 Admin panel and navigate to the "Custom Reports" section. Vous devriez see your custom rapport listed there. Click on it, and you should see the grid with the data you defined.
If everything looks good, congratulations! You've successfully created a custom rapport in Magento 2 Admin. Si vous encounter any problèmes, double-check your code and make sure all fichiers are in the correct locations.
Conclusion
Creating a custom rapport in Magento 2 Admin might seem daunting at first, but by following these étapes, you can easily tailor your rapports to meet your entreprise needs. Whether you're tracking sales, monitoring client behavior, or analyzing product performance, custom rapports can provide the insights you need to make informed decisions. Happy rapporting!