Powered by Blogger.

Hướng dẫn viết PrestaShop Modules

Trước khi viết 1 Modules trong Prestashop, chúng ta cần tìm hiểu Prestashop modules làm việc như thế nào. Tất cả Prestashop modules đều thừa kế từ lớp Module và mỗi module gắn với 1 Hook ( 1 vị trí) cho trước.
Hook là gì?

Hook là một vị trí module đã được khai báo sẵn trong Prestashop. Theo mặc định, Prestashop chỉ có 1 số hooks như sau:
top, header, leftColumn, home, rightColumn, footer, payment, paymentReturn, updateOrderStatus, cart, customerAccount ,productActions ,productTab , productTabContent,...

Bạn có thể tìm hiểu thêm về Prestashop Hooks trong các bài viết sau. Để tạo 1 hook mới, xem thêm tại http://www.prestashop.com/forums/viewthread/12637/#58476.  Bây giờ chúng ta sẽ đi vào phần chính.

Viết module trong PrestaShop
Tôi sẽ viết 1 module mẫu "example" và giải thích luồng xử lý của Prestashop module. Module này sẽ in ra dòng chữ "Hello world!" theo mặc định hoặc in ra nội dung do bạn nhập vào.
File output (example.tpl) hiển thị ở PrestaShop Front Office.

1. Tạo 1 thư mục example bên trong thư mục modules.
2. Tạo 1 file example.tpl để hiển thị nội dung trên trang front office. Sử dụng Microsoft Expression Web, Editplus, Notepad++ hoặc Dreamweaver,...
3. Viết các đoạn mã sau.
<div class="block">
 <h4>Block Example</h4>
 <div class="block_content">
 Example Prestashop module<br />
 <a href="http://www.kenh360.com/">
 www.kenh360.com
 </a> Hello world!<br />
<p>{$content}</p>
 </div>
 </div>

Biến {$module_dir} trả lại đường dẫn thư mục modules. Bạn có thể gán lại giá trị của biến này trong file /config/config.inc.php
.
File cấu hình module example.php trong Prestashop Back Office

1. Tạo file example.php
2. Copy và paste đoạn code dưới đây.

 <?php
 class Example extends Module
 {
// Hàm khởi tạo
 function __construct()
 {
 $this->name = 'Example';
 $this->tab = 'Blocks';
 $this->version = 1.0;

 parent::__construct(); // The parent construct is required for translations

 $this->page = basename(__FILE__, '.php');
 $this->displayName = $this->l('Block example');
 $this->description = $this->l('Add a example block');
 }
// Cài đặt
 function install()
 {
 if (!parent::install())
 return false;
 if (!$this->registerHook('leftColumn'))
 return false;
 return true;
 }
// Hàm gỡ cài đặt
public function uninstall()
 {
 if (!parent::uninstall()) return false;
 if( !Configuration::deleteByName('example_content')) return false;
 return true;

 }
Hiển thị nội dung
 /**
 * Returns module content
 *
 * @param array $params Parameters
 * @return string Content
 */
// Gắn vào 1 hook: cột trái LeftColumn hoặc cột phải RightColumn
 function hookLeftColumn($params)
 {
global $smarty;
 //$smarty->output_charset = 'HTML';
 $examplecontent=Configuration::get('example_content');
 //$examplecontent=html_entity_decode($examplecontent);
 $smarty->assign('content',$examplecontent);
 return $this->display(__FILE__, 'example.tpl');
 }

function hookRightColumn($params)
 {
 return $this->hookLeftColumn($params);
 }


// Lấy nội dung tại Back Office
public function getContent()
 {
 $output = '<h2>'.$this->displayName.'</h2>';
 if (Tools::isSubmit('submit') )
 {

 Configuration::updateValue('example_content', $gai, true);
 $output .= '
 <div class="confirm">    
 '.$this->l('Settings updated').'
 </div>';
 }
 return $output.$this->displayForm();
 }
// Hiển thị form nhập nội dung tại Back Office
 public function displayForm()
 {
 $output = '
 <form action="'.$_SERVER['REQUEST_URI'].'" method="post">
 <fieldset><legend>'.$this->l('Settings').'</legend>
 <label>'.$this->l('Your content').'</label>
 <div class="margin-form">
 <textarea name="example_content" cols="90" rows="10" />'.Tools::getValue('example_content', Configuration::get('example_content')).'</textarea>
 </div>
 <input type="submit" name="submit" value="'.$this->l('Update settings').'" class="button" />
 </fieldset>
 </form>';
 return $output;
 }
 }
 ?>
Tham khảo từ marghoobsuleman.com
Download Prestashop Module mẫu.
    Blogger Comment
    Facebook Comment