Creating Shipping Module

From osCommerce Wiki
Revision as of 17:54, 3 February 2022 by Admin (talk | contribs) (Created page with "'''1. File creation''' First you need to create the '''file''' for the new module and place it in the following directory. '''/lib/common/modules/orderShipping/''' For exa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

1. File creation


First you need to create the file for the new module and place it in the following directory.

/lib/common/modules/orderShipping/

For example, let us call this new shipping module custom.php.

The file skeleton should look in the following way:

----------------------------------------------------------------------------------------------------------------------------------------------------

<?php

/**

* namespace

*/

namespace common\modules\orderShipping;

/**

* used classes

*/

use common\classes\modules\ModuleShipping;

use common\classes\modules\ModuleStatus;

use common\classes\modules\ModuleSortOrder;

/**

* class declaration

*/

class custom extends ModuleShipping {

/**

* variables

*/

public $enabled = false;

public $code = 'custom';

public $icon = '';

public $title = '';

public $description = '';


/**

* default values for translation

*/

protected $defaultTranslationArray = [

'MODULE_CUSTOM_TEXT_TITLE' => 'Custom rates',

'MODULE_CUSTOM_TEXT_DESCRIPTION' => 'My custom rates',

];

/**

* class constructor

*/

function __construct() {

parent::__construct();

$this->title = MODULE_CUSTOM_TEXT_TITLE;

$this->description = MODULE_CUSTOM_TEXT_DESCRIPTION;


if (!defined('MODULE_CUSTOM_STATUS')) {

$this->enabled = false;

return false;

}


$this->sort_order = MODULE_CUSTOM_SORT_ORDER;

$this->enabled = ((MODULE_CUSTOM_STATUS == 'True') ? true : false);

$this->tax_class = MODULE_CUSTOM_TAX_CLASS;

}

/**

* you custom methods

*/

function quote($method = '') {

$methods = [];


$methods[] = [

'id' => 1,

'title' => 'Today',

'cost' => 10

];

$methods[] = [

'id' => 2,

'title' => 'Next day',

'cost' => 5,

];

$this->quotes = [

'id' => $this->code,

'module' => MODULE_CUSTOM_TEXT_TITLE,

'methods' => $methods

];

if ($this->tax_class > 0) {

$this->quotes['tax'] = \common\helpers\Tax::get_tax_rate($this->tax_class, $this->delivery['country']['id'], $this->delivery['zone_id']);

}

if (tep_not_null($this->icon)) {

$this->quotes['icon'] = tep_image($this->icon, $this->title);

}

return $this->quotes;

}

/**

* configuration fields

*/

public function configure_keys() {

return array(

'MODULE_CUSTOM_STATUS' => array(

'title' => 'Enable My Custom Shipping',

'value' => 'True',

'description' => 'Do you want to enable shipping?',

'sort_order' => '0',

'set_function' => 'tep_cfg_select_option(array(\'True\', \'False\'), ',

),

'MODULE_CUSTOM_TAX_CLASS' => array(

'title' => 'Tax Class',

'value' => '0',

'description' => 'Use the following tax class on the shipping fee.',

'sort_order' => '0',

'use_function' => '\\common\\helpers\\Tax::get_tax_class_title',

'set_function' => 'tep_cfg_pull_down_tax_classes(',

),

'MODULE_CUSTOM_SORT_ORDER' => array(

'title' => 'Sort Order',

'value' => '0',

'description' => 'Sort order of display.',

'sort_order' => '100',

),

);

}

public function describe_status_key() {

return new ModuleStatus('MODULE_CUSTOM_STATUS', 'True', 'False');

}

public function describe_sort_key() {

return new ModuleSortOrder('MODULE_CUSTOM_SORT_ORDER');

}


}

--------------------------------------------------------------------------------------------------------------------------------------------------

Note: Pay attention to the mandatory elements:

  • namespace indicating the class placement
  • use modules classes used for the installation creation
  • class declaration with the mandatory inheritance from ModuleShipping
  • variables required for your module
  • keys for translation
  • constructor doing initialization
  • quote function for providing the list of methods
  • tax_class support
  • the minimum set of configuration fields
  • describe_status_key function responsible for the condition
  • describe_sort_key function responsible for the sort order

It is not recommended to close php tag to avoid showing the non-printable symbols on the pages.


2. Module installation


After you accessed the admin area of your website click on Modules and Shipping tabs. Switch on Show inactive and Show not installed switches, find your module and click on Install button.

Image 730.png