In our previous blog, we learned to create a module in Opencart. Now, we learn to build a simple module in Opencart. Shipping is one of the major parts of an e-commerce website. So, we must learn this part for the Opencart.
Create A Language File For The Shipping Method
In Order To Proceed, you will need to create a new Language file for your module. Subsequently, a file called “my_shipping.php” will be located in the “/admin/language/en-gb/extension/shipping/” directory.
<?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'] = 'My Shipping'; // Text $_['text_extension'] = 'Extensions'; $_['text_success'] = 'Success: You have modified My shipping!'; $_['text_edit'] = 'Edit My Shipping'; // Entry $_['entry_cost'] = 'Cost'; $_['entry_tax_class'] = 'Tax Class'; $_['entry_status'] = 'Status'; $_['entry_sort_order'] = 'Sort Order'; // Error $_['error_permission'] = 'Warning: You do not have permission to modify My shipping!';
Create A Controller File For The Shipping Method
In Order To Proceed, you will need to create a new PHP Controller file. As a result, you can create a file called “my_shipping.php” in the “/admin/controller/extension/shipping/” directory.
<?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 ControllerExtensionShippingMyShipping extends Controller { private $error = array(); public function index() { $this->load->language('extension/shipping/my_shipping'); $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('shipping_my_shipping', $this->request->post); $this->session->data['success'] = $this->language->get('text_success'); $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=shipping', true)); } 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', 'user_token=' . $this->session->data['user_token'], true) ); $data['breadcrumbs'][] = array( 'text' => $this->language->get('text_extension'), 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=shipping', true) ); $data['breadcrumbs'][] = array( 'text' => $this->language->get('heading_title'), 'href' => $this->url->link('extension/shipping/my_shipping', 'user_token=' . $this->session->data['user_token'], true) ); $data['action'] = $this->url->link('extension/shipping/my_shipping', 'user_token=' . $this->session->data['user_token'], true); $data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=shipping', true); if (isset($this->request->post['shipping_my_shipping_cost'])) { $data['shipping_my_shipping_cost'] = $this->request->post['shipping_my_shipping_cost']; } else { $data['shipping_my_shipping_cost'] = $this->config->get('shipping_my_shipping_cost'); } if (isset($this->request->post['shipping_my_shipping_tax_class_id'])) { $data['shipping_my_shipping_tax_class_id'] = $this->request->post['shipping_my_shipping_tax_class_id']; } else { $data['shipping_my_shipping_tax_class_id'] = $this->config->get('shipping_my_shipping_tax_class_id'); } $this->load->model('localisation/tax_class'); $data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses(); if (isset($this->request->post['shipping_my_shipping_status'])) { $data['shipping_my_shipping_status'] = $this->request->post['shipping_my_shipping_status']; } else { $data['shipping_my_shipping_status'] = $this->config->get('shipping_my_shipping_status'); } if (isset($this->request->post['shipping_my_shipping_sort_order'])) { $data['shipping_my_shipping_sort_order'] = $this->request->post['shipping_my_shipping_sort_order']; } else { $data['shipping_my_shipping_sort_order'] = $this->config->get('shipping_my_shipping_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('extension/shipping/my_shipping', $data)); } protected function validate() { if (!$this->user->hasPermission('modify', 'extension/shipping/my_shipping')) { $this->error['warning'] = $this->language->get('error_permission'); } return !$this->error; } }
Create A View File For The Shipping Method
In Order To Proceed, you will need to create a new PHP Twig file. As a consequence, you can create a file called “my_shipping.php” in the “/admin/view/template/extension/shipping/” directory.
{# * 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 #} {{ header }}{{ column_left }} <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <button type="submit" form="form-shipping" data-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fa fa-save"></i></button> <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default"><i class="fa fa-reply"></i></a></div> <h1>{{ heading_title }}</h1> <ul class="breadcrumb"> {% for breadcrumb in breadcrumbs %} <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li> {% endfor %} </ul> </div> </div> <div class="container-fluid"> {% if error_warning %} <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ error_warning }} <button type="button" class="close" data-dismiss="alert">×</button> </div> {% endif %} <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"><i class="fa fa-pencil"></i> {{ text_edit }}</h3> </div> <div class="panel-body"> <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-shipping" class="form-horizontal"> <div class="form-group"> <label class="col-sm-2 control-label" for="input-cost">{{ entry_cost }}</label> <div class="col-sm-10"> <input type="text" name="shipping_my_shipping_cost" value="{{ shipping_my_shipping_cost }}" placeholder="{{ entry_cost }}" id="input-cost" class="form-control" /> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" for="input-tax-class">{{ entry_tax_class }}</label> <div class="col-sm-10"> <select name="shipping_my_shipping_tax_class_id" id="input-tax-class" class="form-control"> <option value="0">{{ text_none }}</option> {% for tax_class in tax_classes %} {% if tax_class.tax_class_id == shipping_my_shipping_tax_class_id %} <option value="{{ tax_class.tax_class_id }}" selected="selected">{{ tax_class.title }}</option> {% else %} <option value="{{ tax_class.tax_class_id }}">{{ tax_class.title }}</option> {% endif %} {% endfor %} </select> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" for="input-status">{{ entry_status }}</label> <div class="col-sm-10"> <select name="shipping_my_shipping_status" id="input-status" class="form-control"> {% if shipping_my_shipping_status %} <option value="1" selected="selected">{{ text_enabled }}</option> <option value="0">{{ text_disabled }}</option> {% else %} <option value="1">{{ text_enabled }}</option> <option value="0" selected="selected">{{ text_disabled }}</option> {% endif %} </select> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" for="input-sort-order">{{ entry_sort_order }}</label> <div class="col-sm-10"> <input type="text" name="shipping_my_shipping_sort_order" value="{{ shipping_my_shipping_sort_order }}" placeholder="{{ entry_sort_order }}" id="input-sort-order" class="form-control" /> </div> </div> </form> </div> </div> </div> </div> {{ footer }}
So, we have completed building our simple shipping module. now, we will install the shipping by heading to Extensions-> Extensions-> Shipping-> My Shipping from the admin panel, and after installing you can configure the shipping by enabling it.
Configuration
Therefore, the admin can easily set the module as per the business requirement by navigating via “Extensions-> Extensions-> Shipping-> My Shipping“.
Once you have installed the My Shipping Module, you can then edit the module and add the Shipping Cost.
- Cost – Furthermore, the admin is required to set the cost for this Shipping method.
- Tax Class: Tax classes are used in case you want to put any kind of tax on your shipping.
- Status– Moreover, the admin only needs to enable or disable the module as per the requirement in the Status section.
- Sort Order: Order at which the My Shipping will come among other shipping methods or shipping modules.
Create A Language File For The Front End
In order To Proceed, you will need to create a new Language file. Consequently, you can create a file called “my_shipping.php” in the “/catalog/language/en-gb/extension/shipping/” directory.
<?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 */ // Text $_['text_title'] = 'My Shipping Rate'; $_['text_description'] = 'My Shipping Rate';
Having completed the previous step, we will now proceed to the model file of the shipping, which is the most important file as all the calculations are conducted in this file itself.
Now we need to create a file for the Front end so the Customer can interact with our module.
Create A Model File For The Front End
You will need to create a new PHP file for your module, Consequently, you can create a file called “my_shipping.php” in the”/catalog/model/extension/shipping/” directory.
<?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 ModelExtensionShippingMyShipping extends Model { public function getQuote($address) { $this->load->language('extension/shipping/my_shipping'); $method_data = array(); $quote_data = array(); $quote_data['my_shipping'] = array( 'code' => 'my_shipping.my_shipping', 'title' => $this->language->get('text_description'), 'cost' => $this->config->get('shipping_my_shipping_cost'), 'tax_class_id' => $this->config->get('shipping_my_shipping_tax_class_id'), 'text' => $this->currency->format($this->tax->calculate($this->config->get('shipping_my_shipping_cost'), $this->config->get('shipping_my_shipping_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency']) ); $method_data = array( 'code' => 'my_shipping', 'title' => $this->language->get('text_title'), 'quote' => $quote_data, 'sort_order' => $this->config->get('shipping_my_shipping_sort_order'), 'error' => false ); return $method_data; } }
Front End For Buyers At The Checkout Page
That is all for you How to create a simple shipping method in the Opencart.
If you need custom Opencart Development services then feel free to reach us. And also explore our exclusive range of Opencart Modules.
!!Have a Great Day Ahead!!
Be the first to comment.