Creating Shipping Module

From osCommerce Wiki
Jump to navigation Jump to search

Note: This manual is for osCommerce v4.


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

Check the keys and if the module is switched on. If you have the section Available for in your version fill it in and save the changes.

Image 729.png


3. Testing


If you have a few sales channels make sure you apply your settings to the required one. Check the list of your modules.

Image 728.png


If you followed all the recommendations the information on the front end will be as follows.

Image 727.png


As it is seen on the previous screenshot the tax was not applied to the shipping charge. To fix it change the tax class to the required value in the module settings.

Image 726.png


After that the corresponding values on the front end are increased by 20%.

Image 731.png


4. Conclusion


This example is the simple one. In most cases the provided solutions should meet your requirements. However, this example gives you an idea how to prepare for using more complicated structures that use API for getting rates. If you are familiar with old modules it will not be difficult for you to update them.