在 magento2 後臺展示 3D 圖片點選數統計,且資料可以匯出為 csv 檔案。大概效果如下:
一、在 Applet\etc\adminhtml\menu.xml
中增加 menu
選單。
<!-- product click list -->
<add id="May_Applet::product" title="Product Click" translate="title" module="May_Applet" sortOrder="25" parent="May" resource="May_Applet::product"/>
<add id="May_Applet::click_list" title="Product Click List" translate="title" module="May_Applet" sortOrder="50" parent="May_Applet::product" action="applet/click/lists" resource="May_Applet::click_list"/>
二、在 Applet\etc\acl.xml
中增加下面程式碼:
<resource id="May_Applet::click_list" title="Product Click List" sortOrder="20"/>
三、增加檔案 Applet\view\adminhtml\templates\click\lists.phtml
,程式碼如下:
<form class="form" action="<?php echo $block->getPostUrl() ?>" method="post" enctype="multipart/form-data">
<?php
$resultData = $block->getContent();
$exportUrl = $block->getExportUrl();
?>
<input name="form_key" type="hidden" value="<?php echo $block->getFormKey() ?>" />
<div class="page-main-actions">
<div class="page-actions-placeholder"></div>
<div class="page-actions" data-ui-id="page-actions-toolbar-content-header">
<div class="page-actions-inner" data-title="<?php echo __('Product Click List');?>">
<div class="page-actions-buttons">
<button id="check-again" title="check again" type="button"
onclick="window.location.href='<?php echo $exportUrl;?>';"
class="action- scalable save primary ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
data-form-role="save" data-ui-id="save-button" role="button" aria-disabled="false"><span
class="ui-button-text"><?php echo __('Export Product Click List');?></span></button>
</div>
</div>
</div>
</div>
<table class="data-grid">
<thead>
<tr>
<th class="data-grid-th" style="width:20px;"><?php echo __('ID');?></th>
<th class="data-grid-th" style="width:50px;"><?php echo __('Customer Id');?></th>
<th class="data-grid-th" style="width:200px;"><?php echo __('Openid');?></th>
<th class="data-grid-th" style="width:20px;"><?php echo __('Sku');?></th>
<th class="data-grid-th" style="width:50px;"><?php echo __('Click Time');?></th>
<th class="data-grid-th" style="width:20px;"><?php echo __('Created At');?></th>
<th class="data-grid-th" style="width:20px;"><?php echo __('Updated At');?></th>
</tr>
</thead>
<tbody>
<?php
if ($resultData) :
foreach($resultData as $item):
$id = $item['id'];
$customerId = $item['customer_id'] ? $item['customer_id'] : '';
$Openid = $item['open_id'] ? $item['open_id'] : '';
$sku = $item['sku'] ? $item['sku'] : '';
$clickTime = !empty($item['click_time']) ? $item['click_time'] : '';
$createdAt = !empty($item['created_at']) ? $item['created_at'] : '';
$updatedAt = !empty($item['updated_at']) ? $item['updated_at'] : '';
?>
<tr>
<td><div><?php echo $id;?></div></td>
<td><div><?php echo $customerId;?></div></td>
<td><div><?php echo $Openid;?></div></td>
<td><div><?php echo $sku;?></div></td>
<td><div><?php echo $clickTime;?></div></td>
<td><div><?php echo $createdAt;?></div></td>
<td><div><?php echo $updatedAt;?></div></td>
</tr>
<?php endforeach;
else:?>
<tr><td colspan="8"><?php echo $block->getError();?></td></tr>
<?php
endif;
?>
</tbody>
</table>
</form>
四、增加檔案 Applet\view\adminhtml\layout\applet_click_lists.xml
,程式碼如下:
<block class="May\Applet\Block\Adminhtml\Click\Lists" name="click.lists" template="May_Applet::click/lists.phtml" />
五、增加檔案 Applet\Block\Adminhtml\Click\Lists.php
,程式碼如下:
...
public function getBackUrl()
{
return $this->_urlBuilder->getUrl('*/*/');
}
/**
* @return string
*/
public function getExportUrl()
{
return $this->_urlBuilder->getUrl('applet/click/lists/', ['export' => 1]);
}
/**
* get product click content
*/
public function getContent()
{
$items = $this->productClickCollection
->setOrder('id', 'desc')
->getItems();
$data = [];
foreach ($items as $item) {
$data[] = $item->getData();
}
return $data;
}
/**
* @return |null
*/
public function getError()
{
return $this->_error;
}
...
六、增加檔案 Applet\Controller\Adminhtml\Click\Lists.php
,程式碼如下:
protected function _isAllowed()
{
return $this->_authorization->isAllowed('May_Applet::click_list');
}
public function execute()
{
$params = $this->getRequest()->getParams();
if (!empty($params['export']) && $params['export'] == 1) {
$this->exportWechatMessageLogList();
} else {
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('May_Applet::click_list');
$resultPage->addBreadcrumb(__('Product Click List'), __('Product Click List'));
$resultPage->getConfig()->getTitle()->prepend(__('Product Click List'));
return $resultPage;
}
}
/**
* export reserve CSV file
*/
public function exportWechatMessageLogList()
{
try {
$result = $this->productClickCollection->getData();
$header = [
'id',
'customer_id',
'open_id',
'sku',
'click_time',
'created_at',
'updated_at',
];
$title = 'product_click_list_' . date('YmdHi') . '.csv';
$this->appletHelper->exportToCSV($result, $header, $title);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $this->redirectUrl();
}
}
/**
* return url
* @return \Magento\Framework\Controller\Result\Redirect
*/
protected function redirectUrl()
{
$redirectUrl = '*/*/lists';
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath($redirectUrl);
}
}
以上為主要程式碼,僅在此作為筆記記錄。
本作品採用《CC 協議》,轉載必須註明作者和本文連結