Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

A shipping extension

A shipping method is what the customer chooses at checkout: courier, pickup, post. Each method is an extension of the type shipping. Its main part is a storefront model that looks at the address and the cart and returns the available options with prices.

Files

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

The admin part is a settings page like the one of the module: the namespace ends with Shipping, the route is extension/acme_hello/shipping/courier, the code of the settings is shipping_courier. Fields: shipping_courier_cost, shipping_courier_status, shipping_courier_sort_order.

The model

// extension/acme_hello/catalog/model/shipping/courier.php
namespace Opencart\Catalog\Model\Extension\AcmeHello\Shipping;
 
class Courier extends \Opencart\System\Engine\Model
{
    public function getQuote(array $address): array
    {
        $this->load->language('extension/acme_hello/shipping/courier');
 
        $cost = (float) $this->config->get('shipping_courier_cost');
 
        $quote['courier'] = [
            'code' => 'courier.courier',
            'name' => $this->language->get('text_description'),
            'cost' => $cost,
            'tax_class_id' => 0,
            'text' => $this->currency->format($cost, $this->session->data['currency']),
        ];
 
        return [
            'code' => 'courier',
            'name' => $this->language->get('heading_title'),
            'quote' => $quote,
            'sort_order' => (int) $this->config->get('shipping_courier_sort_order'),
            'error' => false,
        ];
    }
}
// extension/acme_hello/catalog/language/en-gb/shipping/courier.php
$_['heading_title'] = 'Courier';
$_['text_description'] = 'Courier delivery to the door';

The method returns one shipping method with one option. The code of an option is the code of the method, a dot and the key of the option.

Key of an optionMeaning
codeWhat is saved in the order: courier.courier
nameText the customer sees
costPrice as a number, in the default currency
tax_class_idTax class of the delivery, 0 — no tax
textPrice as text in the customer's currency

In releases 4.0.0 and 4.0.1 the text was passed under the key title instead of name. Compare with the built-in method extension/opencart/catalog/model/shipping/flat.php of your copy.

Turning it on

In Extensions → Extensions, type Shipping, install «Courier», open the settings, enter the cost 5 and turn the status on.

Put a product into the cart and go to checkout: index.php?route=checkout/checkout. After the address is entered, the list of shipping methods contains «Courier delivery to the door — $5.00».

Price depending on the cart

The model has access to the cart, so the price can be calculated:

$cost = 5.0 + max(0, $this->cart->getWeight() - 2) * 1.5;

The first two kilograms cost $5, each next one adds $1.50.

When the method is not available

If delivery to the address is impossible, the method returns an empty array — the method does not appear in the list:

if ($address['country_id'] != $this->config->get('config_country_id')) {
    return [];
}

Built-in methods limit the area with a geo zone — a set of countries and regions from System → Localisation → Geo Zones.

Common mistake

The code of the option does not match the pattern: 'code' => 'courier' instead of 'courier.courier'. The method is shown in the list, but after choosing it the checkout reports that the shipping method is invalid: the core splits the code by the dot to find the method and the option.

Thanks for reading!

Donate to the site — we work hard for you.