在本文中,我将演示如何在OpenCart中创建自定义仪表板模块。 仪表板模块可提供有关商店事件和统计信息的高级视图。 尽管您已经在核心本身中找到了某些有用的仪表板模块,但有时您还是想更具体地了解将要显示的信息。 因此,您最终创建了自定义仪表板模块。
今天,我们将创建一个示例仪表板模块,并将其与管理仪表板显示集成。 为简单起见,我们将分店中提供的最新客户数量作为参考。 我们将使用最新版本的OpenCart,并且假设您熟悉OpenCart中的基本模块开发过程。
什么是仪表板模块?
登录到OpenCart的后端后,您将被重定向到屏幕,在该屏幕上您将看到“总订单”,“总销售”,“世界地图”等框。这些框是不同的仪表板模块可提供有关商店中正在发生的事情的有用信息。 任何仪表板模块最常见的用途之一就是提供某种统计信息。
![仪表板模块](https://i-blog.csdnimg.cn/blog_migrate/e544d82a633d90b2a507b55cf9cdc49e.png)
从技术上讲,仪表板模块与OpenCart中的其他模块相似,并且遵循与任何OpenCart模块相同的结构和约定。 但是,使它们与众不同的是它们在后端与仪表板显示器的连接方式。
因此,我们将从定制仪表板模块的常规定制模块开发过程开始。 最后,我们将模块附加到仪表板部分。
创建自定义仪表板模块
继续创建具有以下内容的文件admin/controller/dashboard/recentcustomers.php
。 我们将使用recentcustomers
作为我们的模块名称。
<?php
class ControllerDashboardRecentcustomers extends Controller {
public function index() {
$this->load->language('dashboard/recentcustomers');
$data['heading_title'] = $this->language->get('heading_title');
$data['column_customer_id'] = $this->language->get('column_customer_id');
$data['column_customer_name'] = $this->language->get('column_customer_name');
$data['column_customer_email'] = $this->language->get('column_customer_email');
$data['column_date_added'] = $this->language->get('column_date_added');
$data['text_no_results'] = $this->language->get('text_no_results');
$data['recentcustomers'] = array();
$this->load->model('report/recentcustomers');
$results = $this->model_report_recentcustomers->getRecentCustomers();
foreach ($results as $result) {
$data['recentcustomers'][] = array(
'customer_id' => $result['customer_id'],
'name' => $result['firstname'] . ' ' . $result['lastname'],
'email' => $result['email'],
'date_added' => $result['date_added']
);
}
return $this->load->view('dashboard/recentcustomers.tpl', $data);
}
}
这是一个相当简单的控制器设置! 这里要注意的重要一点是,我们正在加载recentcustomers
模型并调用getRecentCustomers
方法来获取最近的客户。
让我们继续并在admin/language/english/dashboard/recentcustomers.php
快速创建一个语言文件。
<?php
// Heading
$_['heading_title'] = 'Recent Customers';
// Text
$_['column_customer_id'] = 'Customer ID';
$_['column_customer_name'] = 'Customer Name';
$_['column_customer_email'] = 'Customer Email';
$_['column_date_added'] = 'Date Added';
$_['text_no_results'] = 'No customer(s) found.';
同样,这仅是设置将在视图文件中使用的标签。
此外,使用以下内容在admin/model/report/recentcustomers.php
创建模型文件。
<?php
class ModelReportRecentcustomers extends Model {
public function getRecentCustomers() {
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` ORDER BY date_added DESC LIMIT 5");
return $query->rows;
}
}
在模型文件中,我们定义了一个getRecentCustomers
方法,该方法将简单地获取商店中的五个最新客户。
最后,我们将继续在admin/view/template/dashboard/recentcustomers.tpl
创建一个视图文件。
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-users"></i> <?php echo $heading_title; ?></h3>
</div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<td class="text-right"><?php echo $column_customer_id; ?></td>
<td><?php echo $column_customer_name; ?></td>
<td><?php echo $column_customer_email; ?></td>
<td><?php echo $column_date_added; ?></td>
</tr>
</thead>
<tbody>
<?php if ($recentcustomers) { ?>
<?php foreach ($recentcustomers as $customer) { ?>
<tr>
<td class="text-right"><?php echo $customer['customer_id']; ?></td>
<td><?php echo $customer['name']; ?></td>
<td><?php echo $customer['email']; ?></td>
<td><?php echo $customer['date_added']; ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="6"><?php echo $text_no_results; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
在视图中,我们正在遍历客户记录,并以一种很好的响应表格式显示它。
因此,就我们的自定义模块而言就是这样! 您应该已经注意到,该过程与任何自定义模块开发过程完全相同。 在下一部分中,我们将确切地看到自定义模块将如何附加到仪表板部分!
将我们的模块附加到仪表板
要将自定义模块附加到仪表板,我们必须在后端更改几个核心文件。 为了简单起见,我们将查看核心文件中所需的更改。 话虽如此,您应该严格避免直接更改核心文件,而应使用OCMOD,这可以使您以更好的方式进行操作。
继续并在您喜欢的文本编辑器中打开admin/controller/common/dashboard.php
文件。 找到$data['recent'] = $this->load->controller('dashboard/recent');
片段,并在该行旁边添加以下片段。
$data['recentcustomers'] = $this->load->controller('dashboard/recentcustomers');
它用于初始化我们的“最近的客户”块。
此外,编辑位于admin/view/template/common/dashboard.tpl
。 找到<div class="col-lg-8 col-md-12 col-sm-12 col-sx-12"><?php echo $recent; ?></div>
该文件中的<div class="col-lg-8 col-md-12 col-sm-12 col-sx-12"><?php echo $recent; ?></div>
代码段,并在该行之后添加以下代码。
<div class="col-lg-8 col-md-12 col-sm-12 col-sx-12">
<?php echo $recentcustomers; ?>
</div>
因此,我们几乎完成了! 现在,刷新仪表板,您应该能够看到一个漂亮的“ 最近的客户”模块,如以下屏幕截图所示。
![最近客户群](https://cms-assets.tutsplus.com/uploads/users/413/posts/25271/image/create-a-dashboard-module-in-opencart-recent-customers-block.png)
结论
今天,我们已经学习了如何在OpenCart中创建自定义仪表板模块。 这是在商店中显示汇总信息并关注商店中正在发生的事情的一种好方法。
我们在市场上提供了许多OpenCart扩展,这些扩展将有助于扩展您的实施范围,而不仅仅是平台提供的现成功能。
我相信它将对您有用,并且别忘了分享您的反馈和建议!
翻译自: https://code.tutsplus.com/tutorials/create-a-dashboard-module-in-opencart--cms-25271