Tuesday, April 8, 2014

Thank you for buying Getting started with OpenCart Module Development [Getting started with OpenCart Module Development]

About Packt Publishing

Packt, pronounced 'packed', published its first book "Mastering phpMyAdmin for Effective MySQL Management" in April 2004 and subsequently continued to specialize in publishing highly focused books on specific technologies and solutions.
Our books and publications share the experiences of your fellow IT professionals in adapting and customizing today's systems, applications, and frameworks. Our solution based books give you the knowledge and power to customize the software and technologies you're using to get the job done. Packt books are more specific and less general than the IT books you have seen in the past. Our unique business model allows us to bring you more focused information, giving you more of what you need to know, and less of what you don't.


Packt is a modern, yet unique publishing company, which focuses on producing quality, cutting-edge books for communities of developers, administrators, and newbies alike. For more information, please visit our website:www.packtpub.com.

About Packt Open Source

In 2010, Packt launched two new brands, Packt Open Source and Packt Enterprise, in order to continue its focus on specialization. This book is part of the Packt Open Source brand, home to books published on software built around Open Source licences, and offering information to anybody from advanced developers to budding web designers. The Open Source brand also runs Packt's Open Source Royalty Scheme, by which Packt gives a royalty to each Open Source project about whose software a book is sold.

Writing for Packt

We welcome all inquiries from people who are interested in authoring. Book proposals should be sent to author@packtpub.com. If your book idea is still at an early stage and you would like to discuss it first before writing a formal book proposal, contact us; one of our commissioning editors will get in touch with you.
We're not just looking for published authors; if you have strong technical skills but no writing experience, our experienced editors can help you develop a writing career, or simply get some additional reward for your expertise.







lease check www.PacktPub.com for information on our titles


OpenCart 1.4 Template Design
ISBN: 978-1-84951-430-9            Paperback: 328 pages
Over 50 incerdibly effective and quick recipes for building modern eye-catching OpenCart templates
Customize dynamic menus, logos, headers, footers, and every other section using tricks you won't find in the official documentation
A great mix of recipes for beginners, intermediate, and advanced OpenCart template
Develop and customize dynamic, powerful OpenCart templates to make your website stand out from the crowd



Please check www.PacktPub.com for information on our titles


Getting Started with OpenCart Module Development


A
Add Module button effects  16
admin folder
changes  55 featured.php file, exploring  50-52 featured.tpl file, exploring  52 modifications  7, 8 admin module
language files, creating  16
admin section
controller, creating  17-22 controller file, creating at  71-92 files creating, for feedback  64 language file, creating at  64, 89 model file, creating at  65-71 template file, creating at  22-26, 78-92
affiliate  31, 32
C
cache  33 captcha  33, 34 cart  34, 35 cart file
modifications, at frontend  93
catalog folder
changes  55, 57 featured.php file, exploring  53, 54 modifications  8, 10
catalog folder frontend model file, creating at  81 catalog (frontend) module
controller file, creating for  27, 29 language file, creating for  27
Index
template file, creating for  29, 30
code, Featured module
exploring  49
config  35 controller creating, in admin section  17-22
controller file
creating, at admin section  71-92 creating, at frontend  82-88
creating, for catalog (frontend) module  27,
29 currency  36, 37 customer  37-39
D
database  39 database tables for feedback  61- 63
document  40, 42
E
encryption  42
F
Featured module code, exploring  49 featured.php file under catalog folder, exploring  53
featured.tpl file under admin folder, explor-
ing  52
in OpenCart 1.5.5.1, configuring  48, 49
featured.php file
under admin folder, exploring  50-52 under catalog folder, exploring  53, 54 featured.tpl file under admin folder, exploring  52
feedback
about  61 database tables  61-63 files, creating for admin section  64
frontend
cart file, modifications  93, 94 controller file, creating at  82-85 language file, creating at  82 template file, creating at  85-87
G
getForm() function  78 getList() function  74
Global library methods affiliate  31, 32 cache  33 captcha  33, 34 cart  34, 35 config  35 currency  36, 37 customer  37, 39 database  39, 40 document  40-42 encryption  42 language  43 length  43 log  44 mail  44 pagination 44 request  45 response  45 session  45 tax  45 URL  46
user  46, 47 weight  47
H
Hello World Content field  11, 12
Hello World module creating  5, 6
L
language  43 language file
creating, at admin section  64, 89 creating, at frontend  82 creating, for admin module  16 creating, for catalog (frontend) module  27
length  43 log  44
M
mail  44 model file
creating, at admin section  65-71 creating, at catalog folder frontend  81
Model View Controller (MVC) language pattern  5 module
Add Module button, effects  16 admin folder, modifications  7, 8 catalog folder, modifications  8-10 configuring  11, 13 Hello World module, creating  5, 6 installing  10 layouts  13, 14 positions  14
same module, displaying in different layout
15 sort order  15 status  15 uninstalling  16
O
oc_feedback_description table  62 oc_feedback_to_store table  62 off-site payment  58 on-site payment  58 OpenCart  5
OpenCart 1.5.5.1
Featured module, configuring  48, 49 Order Total module  58, 59

P                                                    Tips module
about  88
pagination  44                                                                cart file at frontend, modifications  93
payment module              controller file, creating at admin section  about  57                89-92
off-site payment  58                                                    language file, creating at admin section  89
on-site payment  58                                                       shopping cart page to show tips, 
positions, module                                                                   modifications  94
column bottom  14 template file, creating at admin section  92 column left  14
column right  14                                                          U
column top  14
uninstall()  16
R                                                   update function  73
URL  46
request  45           user  46 response  45
V S
validateForm()function  78
session  45
shipping module              about  54
admin folder, changes  55 weight  47 catalog folder, changes  55, 57
shopping cart page
to show tips, modifications  94
sort order  15
T
tax  45 template file
creating, at admin  22-27 creating, at admin section  92
creating, for catalog (frontend) module  29,
30 creating, for form  78-80 creating, for list  78-80

Creating the template file at the admin section [Creating Custom OpenCart Modules]

Navigate to admin/view/template/total/ and create tips.tpl and insert the following code:
<?php echo $header; ?>
<div id="content">
<div class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<?php echo $breadcrumb['separator']; ?><a href="<?php echo 
  $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
<?php } ?>
</div>
<?php if ($error_warning) { ?>
<div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
<div class="box">
<div class="heading">
<h1><imgsrc="view/image/total.png" alt="" /><?php echo $heading_title;
?></h1>
<div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo    $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
</div>
<div class="content">
<form action="<?php echo $action; ?>" method="post"
enctype="multipart/form-data" id="form">
<table class="form">
<tr>
<td><?php echo $entry_status; ?></td>
<td><select name="tips_status">
<?php if ($tips_status) { ?>
<option value="1" selected="selected"><?php echo $text_enabled; ?></ option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo $text_disabled; ?></ option> <?php } ?>
</select></td>
</tr>
<tr>
<td><?php echo $entry_sort_order; ?></td>
<td><input type="text" name="tips_sort_order" value="<?php echo $tips_ sort_order; ?>" size="1" /></td>
</tr>
</table>
</form>
</div>
</div>
</div>
<?php echo $footer; ?>

Changes made in the cart file at the frontend

Navigate to catalog/view/theme/default/template/checkout/ and open cart. tpl and paste the following code just before the <?php if ($voucher_status) { ?> code.
<?php if ($this->config->get('tips_status')==1) { ?>
<tr class="highlight">
<td><?php if ($next == 'tips') { ?>
<input type="radio" name="next" value="tips" id="use_tips"    checked="checked" />
 <?php } else { ?>
<input type="radio" name="next" value="tips" id="use_tips" />
<?php } ?></td>
<td>Enter the Tips</td>
</tr>
<?php } ?>
The preceding code will show a radio button followed by the Enter the Tips text. On selecting this radio button, div with the id of tips is displayed.
Now just before the <div id="voucher" class="content"> line, paste the following code:
<div id="tips" class="content" style="display: <?php echo ($next 
  == 'tips' ? 'block' : 'none'); ?>;">
<form action="<?php echo $action; ?>" method="post"    enctype="multipart/form-data">        Enter your amount&nbsp;
<input type="text" name="tips" value="" />
<input type="hidden" name="next" value="tips" /> &nbsp;
<input type="submit" value="Apply Tips" class="button" />
</form>
</div>
It shows the Enter your amount form and an Apply Tips button.

Changes in the shopping cart page to show tips

Navigate to catalog/controller/checkout/ and open cart.php. Look for // Voucher and paste the following lines of code before it:
// Tips   
if (isset($this->request->post['tips'])) {
  $this->session->data['tips'] = $this->request->post['tips'];
  $this->session->data['success'] = $this->language
    ->get('text_tips');
  $this->redirect($this->url->link('checkout/cart'));
}
It activates the session for total extension. While installing the Order Total module, it is saved on the extension table as total just like the Tips module gets saved as shown in the following screenshot:


So once the session of tips is activated, the entire total is calculated and we do not need to work out another. We just need to activate the session of the tips, which we have done with the preceding code. With this, our Order Total module is complete.

Summary

In this chapter, we learned the ways to manage data. This was achieved by creating pages, listing it out, inserting the data to the database and retrieving it either to display or to edit, and finally deleting the data. Likewise, we showed you how to list the data at the frontend by making the page. At the end, we created the Order Total Tips module and showed you how it changed the order totals. Using this, you will be able to create modules and pages to manage the data across OpenCart.

The Tips module [Creating Custom OpenCart Modules]

We are creating the Tips module. When the Tips module is activated at the admin section from Admin | Extensions | Order Totals, it will be listed in the Order Totals listing page, and you will see the Tips module activated at Shopping Cart. After entering the amount and clicking on the Apply Tips button, the extra amount is added to the order total, which adds to the total cost of the order.


Creating the language file at the admin section

To create a language file for the Order Total module, we have to create the file at the total folder in the language folder. Navigate to admin/language/english/total/, and create a tips.php file and insert the following lines of code:
<?php
$_['heading_title']    = 'Tips Fee';
$_['text_total']       = 'Order Totals';
$_['text_success'] = 'Success: You have modified tips fee total!';
$_['entry_total']      = 'Order Total:';
$_['entry_fee']        = 'Fee:';
$_['entry_tax_class']  = 'Tax Class:';
$_['entry_status']     = 'Status:';
$_['entry_sort_order'] = 'Sort Order:';
$_['error_permission'] = 'Warning: You do not have permission to modify Tips fee total!'; ?>

Creating the controller file at the admin section

After creating the language file, we now need to create the controller file. Navigate to admin/controller/total/ and create tips.php and insert the following code. Most of the code has already been described, so we will skip the descriptions here.
<?php
class ControllerTotaltips extends Controller {   private $error = array();   public function index() {
    $this->language->load('total/tips');
    $this->document->setTitle($this->language
      ->get('heading_title'));
    $this->load->model('setting/setting');
    if (($this->request->server['REQUEST_METHOD'] == 'POST') &&
    $this->validate()) {
      $this->model_setting_setting->editSetting('tips', $this
        ->request->post);
      $this->session->data['success'] = $this->language
        ->get('text_success');
      $this->redirect($this->url->link('extension/total', 'token='
      . $this->session->data['token'], 'SSL'));
    }
The group column in the database setting table has the value tips as the word
"tips" is passed from $this->model_setting_setting->editSetting('tips', $this->request->post); and therefore each setting value of the Tips module will have the tips value in the groupcolumn. When saved, we will see rows at the setting table as shown in the following screenshot:


The following is the language section part in the controller to assign the variable, which will be used on the template files:
$this->data['heading_title'] = $this->language
  ->get('heading_title');
$this->data['text_enabled'] = $this->language
  ->get('text_enabled');
$this->data['text_disabled'] = $this->language
  ->get('text_disabled');
$this->data['text_none'] = $this->language->get('text_none');
$this->data['entry_status'] = $this->language
  ->get('entry_status');
$this->data['entry_sort_order'] = $this->language
  ->get('entry_sort_order');
$this->data['button_save'] = $this->language->get('button_save');
$this->data['button_cancel'] = $this->language
  ->get('button_cancel');
if (isset($this->error['warning'])) {
  $this->data['error_warning'] = $this->error['warning'];
} else {
  $this->data['error_warning'] = '';
}
Up to this point, the language is loaded to the variable and passed to the template files.
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
  'text'      => $this->language->get('text_home'),
  'href'      => $this->url->link('common/home', 'token=' . $this
    ->session->data['token'], 'SSL'),
  'separator' => false
);
$this->data['breadcrumbs'][] = array(
  'text'      => $this->language->get('text_total'),
  'href'      => $this->url->link('extension/total', 'token=' .
    $this->session->data['token'], 'SSL'),
  'separator' =>' :: '
);
$this->data['breadcrumbs'][] = array(
  'text'      => $this->language->get('heading_title'),
  'href'      => $this->url->link('total/tips', 'token=' . $this
    ->session->data['token'], 'SSL'),
  'separator' =>' :: '
);
Breadcrumbs are created in an array and passed to the template files.
$this->data['action'] = $this->url->link('total/tips', 'token=' .
  $this->session->data['token'], 'SSL');
$this->data['cancel'] = $this->url->link('extension/total',
  'token=' . $this->session->data['token'], 'SSL');
if (isset($this->request->post['tips_status'])) {
  $this->data['tips_status'] = $this->request
    ->post['tips_status'];
} else {
  $this->data['tips_status'] = $this->config->get('tips_status');
}
if (isset($this->request->post['tips_sort_order'])) {
  $this->data['tips_sort_order'] = $this->request
    ->post['tips_sort_order'];
} else {
  $this->data['tips_sort_order'] = $this->config
    ->get('tips_sort_order');
}
$this->template = 'total/tips.tpl';
$this->children = array(
  'common/header',
  'common/footer'
);
$this->response->setOutput($this->render());
}
protected function validate() {
  if (!$this->user->hasPermission('modify', 'total/tips')) {
    $this->error['warning'] = $this->language
      ->get('error_permission');
  }
if (!$this->error) {   return true;
} else {   return false;
}
}
}
?>
Until here, the cancel and form action URL are defined, and the status of the Tips module is assigned as per the active POST method, else from the database config settings. Likewise, a sort order of the Tips module is assigned and the tips.tpl template is rendered.
The validate function is to check whether the user has the permission to modify or not. If they do, only then it returns true, else false.