Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » OpenCart » Tạo mới module hiển thị vị trí mới trong OpenCart

Tạo mới module hiển thị vị trí mới trong OpenCart

Thứ Bảy, 21/03/2015 by Hoàng Quách

Nội dung

  • 1 Tạo vị trí mới (Controller)
  • 2 Tạo vị trí hiển thị module trong Template
  • 3 Hiển thị module ở vị trí mới
  • 4 Gắn vị trí vào Modules
  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Nhiều người học OpenCart chưa biết thêm nhiều ví hiển thị các modules trong OpenCart. Bạn có thể xem mẫu website http://www.marketinsg.com/, kéo thanh cuộn xuống dưới của trang sẽ thấy Facebook like box. Bài viết này mình sẽ hướng dẫn bạn làm được như vậy. Thêm vị trí mới trên website và hiển thị module của bạn luôn ở công việc hay phải làm khi bạn thiết kế template mới bằng OpenCart.

Tạo vị trí mới (Controller)

Đầu tiên, bạn sẽ tạo vị trí mới, có thể tham khảo nội dung file PHP sau đây và lưu với tên chẳng hạn ‘content_footer’. Bạn đặt file này trong thư mục ‘catalog/controller/common/’.

Tiếp theo, bạn copy đoạn code dưới đây vào file:

<?php
class ControllerCommonContentFooter extends Controller {
    public function index() {
        $this->load->model('design/layout');
        $this->load->model('catalog/category');
        $this->load->model('catalog/product');
        $this->load->model('catalog/information');
 
        if (isset($this->request->get['route'])) {
            $route = (string)$this->request->get['route'];
        } else {
            $route = 'common/home';
        }
 
        $layout_id = 0;
 
        if ($route == 'product/category' && isset($this->request->get['path'])) {
            $path = explode('_', (string)$this->request->get['path']);
 
            $layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));
        }
 
        if ($route == 'product/product' && isset($this->request->get['product_id'])) {
            $layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
        }
 
        if ($route == 'information/information' && isset($this->request->get['information_id'])) {
            $layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
        }
 
        if (!$layout_id) {
            $layout_id = $this->model_design_layout->getLayout($route);
        }
 
        if (!$layout_id) {
            $layout_id = $this->config->get('config_layout_id');
        }
 
        $module_data = array();
 
        $this->load->model('setting/extension');
 
        $extensions = $this->model_setting_extension->getExtensions('module');
 
        foreach ($extensions as $extension) {
            $modules = $this->config->get($extension['code'] . '_module');
 
            if ($modules) {
                foreach ($modules as $module) {
                    if ($module['layout_id'] == $layout_id && $module['position'] == 'content_footer' && $module['status']) {
                        $module_data[] = array(
                            'code'       => $extension['code'],
                            'setting'    => $module,
                            'sort_order' => $module['sort_order']
                        );
                    }
                }
            }
        }
 
        $sort_order = array();
 
        foreach ($module_data as $key => $value) {
            $sort_order[$key] = $value['sort_order'];
        }
 
        array_multisort($sort_order, SORT_ASC, $module_data);
 
        $this->data['modules'] = array();
 
        foreach ($module_data as $module) {
            $module = $this->getChild('module/' . $module['code'], $module['setting']);
 
            if ($module) {
                $this->data['modules'][] = $module;
            }
        }
 
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/content_footer.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/common/content_footer.tpl';
        } else {
            $this->template = 'default/template/common/content_footer.tpl';
        }
 
        $this->render();
    }
}
?>

Đừng lo lắng, đó là file controller, mọi extension được đặt trong ‘content_footer’? Nếu là vậy nó sẽ lấy extension và hiển thị trong file template content_footer.tpl. Điều này có nghĩa chúng ta sẽ cần tạo một file template cho module này.

Tạo vị trí hiển thị module trong Template

Ok, bây giờ bạn sẽ tạo một file template có tên ‘content_footer.tpl’ và đặt vào folder ‘catalog/view/theme/default/template/common/’. Đơn giản chép đoạn code dưới đây vào file template mới bạn vừa tạo ở trên.

<?php foreach ($modules as $module) { ?>
   <?php echo $module; ?>
<?php } ?>

Bạn đã hoàn thành, bước tiếp đến bạn cần hiển thị module lên trang trên frontend.

Hiển thị module ở vị trí mới

Mở và sửa file catalog/controller/common/footer.php, tìm dòng:

$this->render();

Trước nó, thêm vào:

$this->children = array(
   'common/content_footer'
);

Việc thêm dòng mới như trên bạn đã tạo vị trí hiển thị module mới vào footer. Bây giờ, để hiển thị ở phần khu vực footer bạn cần sửa 1 chút template footer. Mở file catalog/view/theme/default/template/common/footer.tpl tìm dòng:

<div id="powered"><?php echo $powered; ?></div>

Thêm dòng sau trước đó:

<?php echo $content_footer; ?>

Như vậy đến công đoạn này bạn phần nào đã hiểu cách làm rồi đúng không. Tất cả những gì còn lại là thêm mọi modules vào vị trí đó.

Gắn vị trí vào Modules

Mở template của một module mà bạn muốn thêm vị trí mới. Chẳng hạn, tôi muốn hiển thị Featured Module ở nội dung footer. Như vậy mình sẽ mở template ‘admin/view/template/module/featured.tpl’.
Tìm đoạn:

<?php if ($module['position'] == 'content_top') { ?>
   <option value="content_top" selected="selected"><?php echo $text_content_top; ?></option>
<?php } else { ?>
   <option value="content_top"><?php echo $text_content_top; ?></option>
<?php } ?>

Thêm trước vào đoạn sau:

<?php if ($module['position'] == 'content_footer') { ?>
   <option value="content_footer" selected="selected">Content Footer</option>
<?php } else { ?>
   <option value="content_footer">Content Footer</option>
<?php } ?>

Tiếp đến, tìm dòng:

html += '      <option value="content_top"><?php echo $text_content_top; ?></option>';

Thêm dòng sau vào trước:

html += '      <option value="content_footer">Content Footer</option>';

bằng cách thêm đoạn code như trên bạn đã có vị trí mới trong ‘content footer’ cho module Featured. Lưu lại file và bây gườ bạn có thể thấy extension mới hiển thị ở vị trí mới rồi. Chúc bạn thành công!

Nếu bạn thấy bài viết này hữu ích, hãy chia sẻ với bạn bè bằng cách nhấn nút chia sẻ ở bên dưới. Theo dõi chúng tôi trên Twitter và Facebook

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: OpenCart Tìm kiếm: opencart position

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.