Opencart获取产品下载

Opencart获取产品下载

本文介绍了Opencart获取产品下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在产品页面中添加一个新选项卡,以允许已登录的客户直接从产品页面下载.

I want to add a new tab into the products page allowing logged in customers to directly download from product page.

乍一看,这似乎很简单,但似乎opencart缺少order_download表中的download_id,所以也许我做错了.

At first this seems simple, but it seems that opencart is missing the download_id from order_download table so maybe I am doing this all wrong.

更新:正在对管理员进行一些研究,发现opencart实际上使用的是文件名而不是download_id,所以我猜想我将不得不使用该文件名.例如:

UPDATE: Been doing some digging into the admin and find that opencart is actually using the filename instead of download_id so guess I will have to use that. E.g:

$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']) . "'");

下面的更新方法是正确的吗?似乎有很多查询,所以不确定是否可以改善?

The updated method below so am I doing this correct? Seems a lot of queries so not sure if this can be improved?

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;
 }

推荐答案

首先,您不必运行更新查询,在下达订单后它会添加数据参见/catalog/model/checkout/order.php::addOrder()

First You don't have to run update query, after Placing order it adds datasee /catalog/model/checkout/order.php::addOrder()

要下载文件

  1. 产品必须具有下载文件
  2. 订单状态应为完整
  3. 剩余下载量应大于0

请参见/catalog/model/account/download.php::getDownloads()

解决方案

/catalog/controller/product/product.php的第170行附近的if ($product_info) {之后添加以下代码

add following code after if ($product_info) { around line 170 of /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添加新功能.如果客户还有该产品的剩余下载量,则这将获取相关的产品下载量.

Default download method fetches all downloads by customer but we need downloads on product by customerso /catalog/model/account/download.php add new function. This will fetch associated downloads of product if customer have remaining download of that product.

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文件中,您可以回显下载

now in product.tpl file you can echo download

<?php
if(isset($downloads)){
  foreach ($downloads as $download) { ?>
      <a href="<?php echo $download['href'];?>"><?php echo $download['name'].'('.$download['remaining'].')' ?></a>
<?php   }
}
 ?>

希望这有帮助

这篇关于Opencart获取产品下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:44