WordPress – OpenCart – SEO – Web Development

OpenCart – chức năng tải các tập tin mà không mua hàng

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:
[php]public function getTotalProductSpecials() {[/php] Thêm code [php]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; }[/php] Tìm file: catalog/controller/product/product.php [php]$this->model_catalog_product->updateViewed($this->request->get['product_id']);[/php] Thêm code: [php]$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']) ); } }[/php] Sau code [php]public function upload() {[/php] Thêm code: [php]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'); } }[/php] Tìm file: catalog/view/theme/default/template/product/product.tpl Sau code: [php]<div id="tabs" class="htabs"><a href="#tab-description"><?php echo $tab_description; ?></a>[/php] Thêm code: [php]<div class="product-info"> <?php if($downloads){ ?> <br> <?php foreach($downloads as $download){ ?> <a href="<?php echo $download['href']; ?>" title=""><?php echo $download['name']; ?><?php echo " (". $download['size'] .")";?></a><br> <?php } ?> <?php } ?> </div>[/php] Vào file: catalog/controller/checkout/login.php. Thay code: [php]$this->data['guest_checkout'] = ($this->config->get('config_guest_checkout') && !$this->config->get('config_customer_price') && !$this->cart->hasDownload());[/php] Thành code: [php]$this->data['guest_checkout'] = ($this->config->get('config_guest_checkout') && !$this->config->get('config_customer_price') );[/php] Tìm file: catalog/controller/checkout/guest.php Thay code: [php]if (!$this->config->get('config_guest_checkout') || $this->config->get('config_customer_price') || $this->cart->hasDownload()) {[/php] Thành code: [php]if (!$this->config->get('config_guest_checkout') ) {[/php] Chúc các bạn code chức năng tải các tập tin mà không mua hàng thành công!

Giới thiệu tác giả

Giới thiệu tác giả: .

There is 1 Brilliant Comment

Trackback URL | Comments RSS Feed

Gửi bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Top