Creating Order Structure Module: Difference between revisions

From osCommerce Wiki
Jump to navigation Jump to search
(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/orderTotal/''' For exampl...")
 
m (Protected "Creating Order Structure Module" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) [cascading])
(No difference)

Revision as of 12:49, 2 September 2022

1. File creation


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

/lib/common/modules/orderTotal/

For example, let us call this new total module ot_custom_fee.php

The file skeleton should look in the following way:

<?php

/**

* namespace

*/

namespace common\modules\orderTotal;

/**

* used classes

*/

use common\classes\modules\ModuleTotal;

use common\classes\modules\ModuleStatus;

use common\classes\modules\ModuleSortOrder;

use common\helpers\Tax;

/**

* class declaration

*/

class ot_custom_fee extends ModuleTotal {

  /**

    * variables

    */

    public $title;

    public $output;

   

  /**

    * default values for translation

    */    protected $defaultTranslationArray = [

        'MODULE_ORDER_TOTAL_CUSTOM_FEE_TITLE' => 'Custom fee',

        'MODULE_ORDER_TOTAL_CUSTOM_FEE_DESCRIPTION' => 'Custom module description'

    ];

  /**

    * class constructor

    */

    function __construct() {

        parent::__construct();

        $this->code = 'ot_custom_fee';

        $this->title = MODULE_ORDER_TOTAL_CUSTOM_FEE_TITLE;

        $this->description = MODULE_ORDER_TOTAL_CUSTOM_FEE_DESCRIPTION;

        if (!defined('MODULE_ORDER_TOTAL_CUSTOM_FEE_STATUS')) {

            $this->enabled = false;

            return false;

        }

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

        $this->sort_order = MODULE_ORDER_TOTAL_CUSTOM_FEE_SORT_ORDER;

        $this->output = [];

    }

  /**

    * your custom process

    */

    function process($replacing_value = -1, $visible = false) {

        $currencies = \Yii::$container->get('currencies');

        $order = $this->manager->getOrderInstance();

        if (defined('MODULE_ORDER_TOTAL_CUSTOM_FEE_FEE') && MODULE_ORDER_TOTAL_CUSTOM_FEE_FEE > 0) {

            $taxation = $this->getTaxValues(MODULE_ORDER_TOTAL_CUSTOM_FEE_TAX_CLASS, $order);

            $tax_class_id = $taxation['tax_class_id'];

            $tax = $taxation['tax'];

            $tax_description = $taxation['tax_description'];

            $gift_wrap_amount += $order->info['total'] * MODULE_ORDER_TOTAL_CUSTOM_FEE_FEE / 100;

            $gift_wrap_tax_amount += Tax::roundTax(Tax::calculate_tax($gift_wrap_amount, $tax));

            $gift_wrap_amount_inc += Tax::add_tax($gift_wrap_amount, $tax);

            $order->info['tax'] += $gift_wrap_tax_amount;

            $order->info['tax_groups']["$tax_description"] += $gift_wrap_tax_amount;

            $order->info['total'] += ($gift_wrap_amount + $gift_wrap_tax_amount);

            $order->info['total_inc_tax'] += ($gift_wrap_amount + $gift_wrap_tax_amount);

            $order->info['total_exc_tax'] += $gift_wrap_amount;

            parent::$adjusting += $currencies->format_clear($gift_wrap_amount, true, $order->info['currency'], $order->info['currency_value']);

        }

        $this->output[] = array(

            'title' => $this->title . ':',

            'text' => $currencies->format($gift_wrap_amount_inc, true, $order->info['currency'], $order->info['currency_value']),

            'value' => $gift_wrap_amount_inc,

            'text_exc_tax' => $currencies->format($gift_wrap_amount, true, $order->info['currency'], $order->info['currency_value']),

            'text_inc_tax' => $currencies->format(($gift_wrap_amount + $gift_wrap_tax_amount), true, $order->info['currency'], $order->info['currency_value']),

            'tax_class_id' => $tax_class_id,

            'value_exc_vat' => $gift_wrap_amount,

            'value_inc_tax' => ($gift_wrap_amount + $gift_wrap_tax_amount),

            'prefix' => '+',

        );

       

    }

  /**

    * configuration fields

    */

    public function configure_keys() {

        return [

            'MODULE_ORDER_TOTAL_CUSTOM_FEE_STATUS' => [

                'title' => 'Display Custom fee',

                'value' => 'true',

                'description' => 'Do you want to display the Custom fee?',

                'sort_order' => '1',

                'set_function' => 'tep_cfg_select_option(array(\'true\', \'false\'), ',

            ],

            'MODULE_ORDER_TOTAL_CUSTOM_FEE_FEE' => [

                'title' => 'Fee',

                'value' => '0',

                'description' => 'Additional fee in percentage',

                'sort_order' => '2',

            ],

            'MODULE_ORDER_TOTAL_CUSTOM_FEE_TAX_CLASS' => [

                'title' => 'Tax Class',

                'value' => '0',

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

                'sort_order' => '3',

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

                'set_function' => 'tep_cfg_pull_down_tax_classes(',

            ],

            'MODULE_ORDER_TOTAL_CUSTOM_FEE_SORT_ORDER' => [

                'title' => 'Sort Order',

                'value' => '4',

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

                'sort_order' => '4',

            ],

        ];

    }

   

    public function describe_status_key() {

        return new ModuleStatus('MODULE_ORDER_TOTAL_CUSTOM_FEE_STATUS', 'true', 'false');

    }

    public function describe_sort_key() {

        return new ModuleSortOrder('MODULE_ORDER_TOTAL_CUSTOM_FEE_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 ModuleTotal
  • variables required for your module
  • keys for translation
  • constructor doing initialization
  • process function for calculations
  • 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, Order Structure and the required frontend tabs. Switch on Show inactive and Show not installed switches, find your module and click on Install button.

Image 819.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 820.png

3. Testing


If you have a few sales channels make sure you apply your settings to the required one. Make sure the status for the module you installed is switched on.

Image 821.png


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

Image 822.png


As it is seen on the previous screenshot the additional fee in percentage and tax on it were not applied to the total amount. To fix it add the additional fee in percentage and change the tax class to the required value in the module settings.

Image 823.png


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

Image 824.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.