Reading list Switch to dark mode

    Create order total in opencart

    Updated 26 April 2023

    In the series of module development tutorial, we have learned about building modules, shipping method in the previous blogs. Now, we will learn about building an order total.

    Order total is one of the major parts of an e-commerce platform. If we need to show any kind of extra cost in the cart or totals and sub-totals then we have to use the order totals. If we need to give some discount or to add up some shipping cost or tax then we need the order total to come into the process. So, here, we will learn to build the order total.

    If you don’t know what are order totals in the Opencart then you must visit Admin->Extensions->Order Totals (Opencart version 2.2.x and lower). With the list visible, you will get an idea of order totals.

    We will begin with the building of language file named ‘test_total.php’ in the admin->language->en-gb->total. Here’s the code for that language file.

    <?php
    /**
     * Webkul Software
     * 
     * @category Webkul
     * @package Opencart Module Tutorial
     * @author [Webkul] <[<http://webkul.com/>]>
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license https://store.webkul.com/license.html
     */
    // Heading
    $_['heading_title']    = 'Test-Total';
    
    // Text
    $_['text_total']       = 'Order Totals';
    $_['text_success']     = 'Success: You have modified Test-Total total!';
    $_['text_edit']        = 'Edit Test-Total Total';
    
    // Entry
    $_['entry_total']      = 'Order Total';
    $_['entry_fee']        = 'Fee';
    $_['entry_tax_class']  = 'Tax Class';
    $_['entry_status']     = 'Status';
    $_['entry_sort_order'] = 'Sort Order';
    
    // Help
    $_['help_total']       = 'The checkout total the order must reach before this order total becomes active.';
    
    // Error
    $_['error_permission'] = 'Warning: You do not have permission to modify Test-Total total!';

    Now, we will progress on to the controller of the order total. We will build a file named ‘test_total.php’ in the admin->controller->total. Here’s the code for controller file:

    Searching for an experienced
    Opencart Company ?
    Find out More
    <?php
    /**
     * Webkul Software
     * 
     * @category Webkul
     * @package Opencart Module Tutorial
     * @author [Webkul] <[<http://webkul.com/>]>
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license https://store.webkul.com/license.html
     */
    class ControllerTotalTestTotal extends Controller {
    	private $error = array();
    
    	public function index() {
    		$this->load->language('total/test_total');
    
    		$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('test_total', $this->request->post);
    
    			$this->session->data['success'] = $this->language->get('text_success');
    
    			$this->response->redirect($this->url->link('extension/total', 'token=' . $this->session->data['token'], true));
    		}
    
    		$data['heading_title'] = $this->language->get('heading_title');
    
    		$data['text_edit'] = $this->language->get('text_edit');
    		$data['text_enabled'] = $this->language->get('text_enabled');
    		$data['text_disabled'] = $this->language->get('text_disabled');
    		$data['text_none'] = $this->language->get('text_none');
    
    		$data['entry_total'] = $this->language->get('entry_total');
    		$data['entry_fee'] = $this->language->get('entry_fee');
    		$data['entry_tax_class'] = $this->language->get('entry_tax_class');
    		$data['entry_status'] = $this->language->get('entry_status');
    		$data['entry_sort_order'] = $this->language->get('entry_sort_order');
    
    		$data['help_total'] = $this->language->get('help_total');
    
    		$data['button_save'] = $this->language->get('button_save');
    		$data['button_cancel'] = $this->language->get('button_cancel');
    
    		if (isset($this->error['warning'])) {
    			$data['error_warning'] = $this->error['warning'];
    		} else {
    			$data['error_warning'] = '';
    		}
    
    		$data['breadcrumbs'] = array();
    
    		$data['breadcrumbs'][] = array(
    			'text' => $this->language->get('text_home'),
    			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
    		);
    
    		$data['breadcrumbs'][] = array(
    			'text' => $this->language->get('text_total'),
    			'href' => $this->url->link('extension/total', 'token=' . $this->session->data['token'], true)
    		);
    
    		$data['breadcrumbs'][] = array(
    			'text' => $this->language->get('heading_title'),
    			'href' => $this->url->link('total/test_total', 'token=' . $this->session->data['token'], true)
    		);
    
    		$data['action'] = $this->url->link('total/test_total', 'token=' . $this->session->data['token'], true);
    
    		$data['cancel'] = $this->url->link('extension/total', 'token=' . $this->session->data['token'], true);
    
    		if (isset($this->request->post['test_total_fee'])) {
    			$data['test_total_fee'] = $this->request->post['test_total_fee'];
    		} else {
    			$data['test_total_fee'] = $this->config->get('test_total_fee');
    		}
    
    		if (isset($this->request->post['test_total_status'])) {
    			$data['test_total_status'] = $this->request->post['test_total_status'];
    		} else {
    			$data['test_total_status'] = $this->config->get('test_total_status');
    		}
    
    		if (isset($this->request->post['test_total_sort_order'])) {
    			$data['test_total_sort_order'] = $this->request->post['test_total_sort_order'];
    		} else {
    			$data['test_total_sort_order'] = $this->config->get('test_total_sort_order');
    		}
    
    		$data['header'] = $this->load->controller('common/header');
    		$data['column_left'] = $this->load->controller('common/column_left');
    		$data['footer'] = $this->load->controller('common/footer');
    
    		$this->response->setOutput($this->load->view('total/test_total', $data));
    	}
    
    	protected function validate() {
    		if (!$this->user->hasPermission('modify', 'total/test_total')) {
    			$this->error['warning'] = $this->language->get('error_permission');
    		}
    
    		return !$this->error;
    	}
    }

    The backend(admin) code for the order total files is same as of modules, payment and shipping just the folder get changes accordingly.

    Now, we will proceed to the view file and we will create a view file named ‘test_total.tpl’ in the admin->view->template->total. The code for the view file is here:

    <!-- 
    /**
     * Webkul Software
     * 
     * @category Webkul
     * @package Opencart Module Tutorial
     * @author [Webkul] <[<http://webkul.com/>]>
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license https://store.webkul.com/license.html
     */ -->
    <?php echo $header; ?><?php echo $column_left; ?>
    <div id="content">
      <div class="page-header">
        <div class="container-fluid">
          <div class="pull-right">
            <button type="submit" form="form-test-total" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
            <a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
          <h1><?php echo $heading_title; ?></h1>
          <ul class="breadcrumb">
            <?php foreach ($breadcrumbs as $breadcrumb) { ?>
            <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
            <?php } ?>
          </ul>
        </div>
      </div>
      <div class="container-fluid">
        <?php if ($error_warning) { ?>
        <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
          <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
        <?php } ?>
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
          </div>
          <div class="panel-body">
            <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-test-total" class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label" for="input-fee"><?php echo $entry_fee; ?></label>
                <div class="col-sm-10">
                  <input type="text" name="test_total_fee" value="<?php echo $test_total_fee; ?>" placeholder="<?php echo $entry_fee; ?>" id="input-fee" class="form-control" />
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
                <div class="col-sm-10">
                  <select name="test_total_status" id="input-status" class="form-control">
                    <?php if ($test_total_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>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="input-sort-order"><?php echo $entry_sort_order; ?></label>
                <div class="col-sm-10">
                  <input type="text" name="test_total_sort_order" value="<?php echo $test_total_sort_order; ?>" placeholder="<?php echo $entry_sort_order; ?>" id="input-sort-order" class="form-control" />
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    <?php echo $footer; ?>

    So, the backend part is complete with these codes.

    Now, we have to create the front-end file. So, we will create a file named ‘test_total.php’ in catalog->model->total. There will be no controller or view files for the order total same as of shipping module. The code for the model file is here:

    <?php
    /**
     * Webkul Software
     * 
     * @category Webkul
     * @package Opencart Module Tutorial
     * @author [Webkul] <[<http://webkul.com/>]>
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license https://store.webkul.com/license.html
     */
    class ModelTotalTestTotal extends Model {
    	public function getTotal($total) {
    		/**
    		 * condition checking whether the sub-total of the order is greater than zero
    		 */
    		if ($this->cart->getSubTotal() > 0) {
    			$total['totals'][] = array(
    				'code'       => 'test_total', // setting the total's code
    				'title'      => 'Test-Total', // setting the total's title
    				'value'      => $this->config->get('test_total_fee'), // setting the total's value
    				'sort_order' => $this->config->get('test_total_sort_order') // setting the total's sort order
    			);
    
    			$total['total'] += $this->config->get('test_total_fee');
    		}
    	}
    }

    We can access the order total in Extension->Order totals from the admin panel. Now, we have to install the order total named ‘Test-Total’ and then fill the fee and sort order value and then after enabling the status, click on the save button. The sort order will depict the order of the order total among other order totals.

    Now, on putting some product into the cart, the order total will automatically be added. As I have added 5 as fee from the backend so it’s adding $5 in front as well. See image.

    checkout order totals

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home