我想在产品页面中添加一个新标签,允许登录的客户直接从产品页面下载。
起初这看起来很简单,但似乎opencart缺少order_download表中的download_id,所以也许我做错了。
更新:正在深入挖掘管理员并发现opencart实际上是使用文件名而不是download_id所以我猜我必须使用它。例如:
$this->db->query("UPDATE " . DB_PREFIX . "order_download SET `filename` = '" . $this->db->escape($data['filename']) . "', mask = '" . $this->db->escape($data['mask']) . "', remaining = '" . (int)$data['remaining'] . "' WHERE `filename` = '" . $this->db->escape($download_info['filename']) . "'");
下面更新的方法,我这样做是否正确?似乎有很多疑问,所以不确定这是否可以改进?
public function getProductDownloads($product_id) {
$data = array();
$result = $this->db->query("SELECT download_id FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
foreach ($result->rows as $product_download) {
$product_download_query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "download d LEFT JOIN " . DB_PREFIX . "download_description dd ON (d.download_id = dd.download_id) WHERE d.download_id = '" . (int)$product_download['download_id'] . "' AND dd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
foreach ($product_download_query->rows as $download) {
if (!file_exists(DIR_DOWNLOAD . $download['filename'])) continue;
$row = array(
'download_id' => $download['download_id'],
'date_added' => date($this->language->get('date_format_short'), strtotime($download['date_added'])),
'name' => $download['name'],
'href' => ''
);
if($this->customer->isLogged()){
// 1. Get customer orders
$order_download_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download od LEFT JOIN `" . DB_PREFIX . "order` o ON (od.order_id = o.order_id) WHERE o.customer_id = '" . (int)$this->customer->getId(). "' AND o.order_status_id > '0' AND o.order_status_id = '" . (int)$this->config->get('config_complete_status_id') . "' AND (od.remaining = -1 OR od.remaining > 0) AND od.filename = '" . $this->db->escape($download['filename']) . "'");
foreach($order_download_query->rows as $order_download) {
// 2. Check customer ordered product
$order_product_download_query = $this->db->query("SELECT 1 FROM " . DB_PREFIX . "order_product op WHERE op.order_product_id = '" . (int)$order_download['order_product_id']. "' AND op.product_id = '" . (int)$product_id . "'");
if($order_product_download_query->row) {
$row['href'] = $this->url->link('account/download/download', 'order_download_id=' . $order_download['order_download_id'], 'SSL');
$row['remaining'] = $order_download['remaining'];
break;
}
}
}
$data[] = $row;
}
}
return $data;
}
答案 0 :(得分:2)
首先,您不必运行更新查询,在下订单后,它会添加数据
见/catalog/model/checkout/order.php::addOrder()
下载文件
请参阅/catalog/model/account/download.php::getDownloads()
<强>解决方案强>
在if ($product_info) {
/catalog/controller/product/product.php
之后添加以下代码
if ($this->customer->isLogged()) {
$this->load->model('account/download');
$results = $this->model_account_download->getDownloadByProduct($product_id);
foreach ($results as $result) {
if (file_exists(DIR_DOWNLOAD . $result['filename'])) {
$size = filesize(DIR_DOWNLOAD . $result['filename']);
$i = 0;
$suffix = array(
'B',
'KB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB'
);
while (($size / 1024) > 1) {
$size = $size / 1024;
$i++;
}
$this->data['downloads'][] = array(
'order_id' => $result['order_id'],
'name' => $result['name'],
'remaining' => $result['remaining'],
'size' => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
'href' => $this->url->link('account/download/download', 'order_download_id=' . $result['order_download_id'], 'SSL')
);
}
}
}
默认下载方法取消客户的所有下载,但我们需要客户下载产品
所以/catalog/model/account/download.php
添加新功能。如果客户仍在下载该产品,则会获取相关的产品下载。
public function getDownloadByProduct($product_id) {
$query = $this->db->query("SELECT o.order_id, o.date_added, od.order_download_id, od.name, od.filename, od.remaining FROM " . DB_PREFIX . "order_download od LEFT JOIN `" . DB_PREFIX . "order` o ON (od.order_id = o.order_id) LEFT JOIN `" . DB_PREFIX . "order_product` op ON (o.order_id = op.order_id) WHERE o.customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id > '0' AND o.order_status_id = '" . (int)$this->config->get('config_complete_status_id') . "' AND od.remaining > 0 AND op.product_id = ' " . $product_id . " ' ORDER BY o.date_added DESC");
return $query->rows;
}
现在在product.tpl
文件中你可以回复下载
<?php
if(isset($downloads)){
foreach ($downloads as $download) { ?>
<a href="<?php echo $download['href'];?>"><?php echo $download['name'].'('.$download['remaining'].')' ?></a>
<?php }
}
?>
希望这会有所帮助