Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

An order total extension

The sum of an order is assembled from lines: products subtotal, shipping, coupon, tax, total. Each line is a separate extension of the type total. To add a fee or a discount of your own, you write one more such extension — here it is a fee for small orders.

Files

A total consists of a settings page in the admin panel and a model on the storefront:

extension/acme_hello/
├── admin/
│   ├── controller/total/small_order.php
│   ├── language/en-gb/total/small_order.php
│   └── view/template/total/small_order.twig
└── catalog/
    ├── model/total/small_order.php
    └── language/en-gb/total/small_order.php

The admin part is written exactly like the settings page of the module, only the namespace ends with Total, the route is extension/acme_hello/total/small_order and the code of the settings is total_small_order. Two fields are needed: total_small_order_status and total_small_order_sort_order.

The model

// extension/acme_hello/catalog/model/total/small_order.php
namespace Opencart\Catalog\Model\Extension\AcmeHello\Total;
 
class SmallOrder extends \Opencart\System\Engine\Model
{
    public function getTotal(array &$totals, array &$taxes, float &$total): void
    {
        if ($this->cart->getSubTotal() >= 20) {
            return;
        }
 
        $this->load->language('extension/acme_hello/total/small_order');
 
        $fee = 3.0;
 
        $totals[] = [
            'extension' => 'acme_hello',
            'code' => 'small_order',
            'title' => $this->language->get('text_small_order'),
            'value' => $fee,
            'sort_order' => (int) $this->config->get('total_small_order_sort_order'),
        ];
 
        $total += $fee;
    }
}

The core calls getTotal() of every enabled total in turn. The method receives three values by reference: the list of lines, the taxes and the running sum. It adds its line and increases the sum.

// extension/acme_hello/catalog/language/en-gb/total/small_order.php
$_['text_small_order'] = 'Small order fee';

Turning it on

  1. Open Extensions → Extensions and choose the type Order Totals.
  2. Install «Small order fee», open its settings, turn the status on and set the sort order.

Put a product cheaper than $20 into the cart and open index.php?route=checkout/cart — under the subtotal there is the line «Small order fee $3.00», and the total is larger by this sum.

If the new item is missing from the list, rebuild the archive and install the package again in Extensions → Installer: the list of files of a package is recorded during installation.

Sort order

Lines are calculated in the order of the sort_order setting, so its value matters:

LineTypical order
Sub-Total1
Shipping3
Taxes5
Total9

A fee with the order 2 is calculated after the products and before the tax. See the real values of your shop in the settings of each total.

A discount

A discount is the same line with a negative value: 'value' => -5.0 and $total -= 5.0.

Common mistake

The line is added to $totals, but $total is not changed. The cart shows the fee, and the final sum stays the same — the customer pays less than the lines add up to. Every line added to the list must also change the running sum by the same value.

Thanks for reading!

Donate to the site — we work hard for you.