Chức năng tải các tập tin mà không mua hàng trên OpenCart

18
2663

Đã test trên OpenCart 1.5.6.1, nếu các bạn test thành công trên phiên bản khác thì comment nhé.

Trong bài viết này mình hướng dẫn các bạn show file pdf  tập tin trong trang chi tiết sản phẩm. Khách hàng có thể download không cần đăng ký mua như mặc định của OpenCart.

OpenCart là một mã nguồn phong phú chức năng. Nó cho phép bạn upload nhiều file và bán chúng như là một sản phẩm thông thường.  Đó là người mua được mua cùng một tập tin này trong tài khoản cá nhân của họ có thể tải về tập tin đó sau khi đã thanh toán. Nhưng đôi khi bạn cần phải thực hiện một cái gì đó khác. Ví dụ, các file tập tin không phải là hàng hóa buôn bán, nó chỉ là tài liệu hướng dẫn, chú thích, … Làm thế nào có thể cho phép người mua trước khi thực hiện mua hàng, tải về các hướng dẫn sử dụng?Những gì để nói về danh sách giá, các tài liệu pdf và các tài liệu thông tin khác …

Tìm file: catalog/model/catalog/product.php

Sau code:
public function getTotalProductSpecials() {

Thêm code

public function getDownloads($product_id) {

 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_download pd LEFT JOIN " . DB_PREFIX . "download d ON(pd.download_id=d.download_id) LEFT JOIN " . DB_PREFIX . "download_description dd ON(pd.download_id=dd.download_id) WHERE product_id = '" . (int)$product_id . "' AND dd.language_id = '" . (int)$this->config->get('config_language_id')."'");

 return $query->rows;
 }

 public function getDownload($product_id, $download_id) {
 $download="";
 if($download_id!=0)$download=" AND d.download_id=".(int)$download_id;
 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_download pd LEFT JOIN " . DB_PREFIX . "download d ON(pd.download_id=d.download_id) LEFT JOIN " . DB_PREFIX . "download_description dd ON(pd.download_id=dd.download_id) WHERE product_id = '" . (int)$product_id . "' ".$download." AND dd.language_id = '" . (int)$this->config->get('config_language_id')."'");

 return $query->row;
 }

Tìm file: catalog/controller/product/product.php

$this->model_catalog_product->updateViewed($this->request->get['product_id']);

Thêm code:

$this->data['downloads'] = array();

$results = $this->model_catalog_product->getDownloads($this->request->get['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(
'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
'name' => $result['name'],
'size' => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
'href' => $this->url->link('product/product/download', 'product_id='. $this->request->get['product_id']. '&download_id=' . $result['download_id'])
);
}
}

Sau code

public function upload() {

Thêm code:

public function download() {

$this->load->model('catalog/product');

if (isset($this->request->get['download_id'])) {
$download_id = $this->request->get['download_id'];
} else {
$download_id = 0;
}

if (isset($this->request->get['product_id'])) {
$product_id = $this->request->get['product_id'];
} else {
$product_id = 0;
}

$download_info = $this->model_catalog_product->getDownload($product_id, $download_id);

if ($download_info) {
$file = DIR_DOWNLOAD . $download_info['filename'];
$mask = basename($download_info['mask']);

if (!headers_sent()) {
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));

readfile($file, 'rb');

//$this->model_account_download->updateRemaining($this->request->get['download_id']);

exit;
} else {
exit('Error: Could not find file ' . $file . '!');
}
} else {
exit('Error: Headers already sent out!');
}
} else {
$this->redirect(HTTP_SERVER . 'index.php?route=account/download');
}
}

Tìm file: catalog/view/theme/*/template/product/product.tpl, * là bất kỳ theme nào, bạn có thể đúng đường dẫn theme đang dùng.Sau code:

<a href="#tab-tags"><?php echo $text_tags; ?></a>

Thêm code:

<?php } ?>
<?php if($downloads){ ?>
<a href="#tab-downloads"><?php echo "Downloads"; ?></a>

Sau code:

<?php if ($products) { ?>

Thêm code:

<?php if($downloads){ ?>
 <div id="tab-downloads" class="tab-content">
 <?php foreach($downloads as $download){ ?>

 <a href="<?php echo $download['href']; ?>" title=""><?php echo $download['name']; ?><?php echo " (". $download['size'] .")";?></a><br>

 <?php } ?>
 </div>
 <?php } ?>

Vào file: catalog/controller/checkout/login.php.Thay code:

$this->data['guest_checkout'] = ($this->config->get('config_guest_checkout') && !$this->config->get('config_customer_price') && !$this->cart->hasDownload());

Thành code:

$this->data['guest_checkout'] = ($this->config->get('config_guest_checkout') && !$this->config->get('config_customer_price') );

Tìm file: catalog/controller/checkout/guest.phpThay code:

if (!$this->config->get('config_guest_checkout') || $this->config->get('config_customer_price') || $this->cart->hasDownload()) {

Thành code:

if (!$this->config->get('config_guest_checkout') ) {

Nguồn từ sacmauweb.com. Mình đã chỉnh sửa một chút để phù hợp với bản OpenCart 1.5.6.1

Muốn download luôn module dạng Vqmod thì bơi vào đây nhé! 😀

Cài đặt cho phép download

Có thể bạn hiển thị được link download nhưng không tải được vì bạn chưa cho phép tải, các bạn cấu hình lại như sau

  1. Đăng nhập vào trang quản trị
  2. Trên menu chọn SYSTEM » SETTINGS
  3. Sau đó chọn edit tại STORE  bạn muốn cấu hình.
  4. Nhấn vào tab OPTION, sau đó tìm đến mục ALLOW DOWNLOADS

Cấu hình cho phép tải file trên opencart

5/5 - (2 bình chọn)

18 BÌNH LUẬN

  1. Cảm ơn bạn đã chia sẻ,
    mình đã làm theo hướng dẫn tới đoạn tìm dòng này nhưng không thấy :

    Hy vọng bạn giúp đỡ, site mình là http://vken.com.vn

  2. À, sorry bạn, mình làm được rồi, nhưng nó chỉ hiển thị lên thôi chứ click vào thì không download được, bạn xem giúp mình cái link này nhé, tks http://vken.com.vn/thiet-bi-kiem-soat-bang-the-tu-soyal-ar-725e

  3. Thư mục chứa file của mình là http://vken.com.vn/download/
    và chmod đang là 755
    Link down thì là : http://vken.com.vn/product/product/download&product_id=91&download_id=1
    Không hiểu sai chỗ nào nhỉ, mình mới vọc về opencart nên gà quá, hy vọng bạn có thể giúp mình đc, tks nhiều !

BÌNH LUẬN

Nội dung bình luận
Tên của bạn là gì?