Tuesday, April 8, 2014

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.

No comments:

Post a Comment