John Magnolia
John Magnolia

Reputation: 16793

Opencart get product downloads

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

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.

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

Upvotes: 0

Views: 1887

Answers (1)

Ramesh
Ramesh

Reputation: 1194

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

To download files

  1. Product Must have download File
  2. Order Status should be Complete
  3. Download remaining should be more than 0

see /catalog/model/account/download.php::getDownloads()

Solution

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

Default download method fetches all downloads by customer but we need downloads on product by customer so /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;
}

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

Hope This helps

Upvotes: 2

Related Questions