Skip to content

Developer notes

The add-on never calls a carrier API. For every queue item it calls a carrier adapter, and every built-in adapter calls the public PHP API of a Kavio Labs label add-on:

Adapter code Label add-on Call What a rule can pass
sendcloud kv_sendcloud Shipments::createLabel($order_id, ['option_code', 'parcels', 'weight_kg']) Sendcloud shipping option code, 1–15 parcels, parcel weight
postnord kv_postnord Shipments::createLabel($order_id, ['parcels', 'weight']) 1–10 parcels, parcel weight
gls kv_gls Shipments::createLabel($order_id, $count) 1–99 parcels
dhl_express kv_dhl_express Shipments::createLabel($order_id, ['product' => ...]) DHL Express product code
ups_rest kv_ups_rest Shipments::createLabel($order_id, ['service_type' => ...]) UPS service code
fedex_rest kv_fedex_rest Shipments::createLabel($order_id, ['service_type' => ...]) FedEx service type
canadapost kv_canadapost Shipments::createLabel($order_id, ['service_code' => ...]) Canada Post service code
dpd, inpost, packeta kv_dpd, kv_inpost, kv_packeta Shipments::createLabel($order_id) (service from the order’s method)

Service lists come from each add-on’s own Installer::services(). Label files are read back with the add-on’s Shipments::label() (DHL Express: Shipments::document() of the documents of type label; UPS and FedEx: every LABEL piece). Shipment rows that are cancelled, still pending, of unknown result or return labels are never printed.

Every Kavio label add-on is idempotent per order: when it already holds an active shipment for the order it returns it instead of buying a second label. On top of that the queue takes a MySQL named lock per order and claims an item atomically (UPDATE ... WHERE state IN ('Q', 'F')), so a double click, two administrators and the cron never ask for the same label at the same time. A claim that never finished (crash, timeout) is released to Failed after 15 minutes; a retry then gets the label the add-on already made. Before every create the queue also asks the other adapters for shipments of the order (liveRows(), any status but cancelled): an order with a shipment at another label add-on is refused instead of buying a second label at a second carrier. Label files still missing 24 hours after the create move the item to Failed (column wait_since).

Multi-Vendor: vendor rules of the label add-ons

Section titled “Multi-Vendor: vendor rules of the label add-ons”

The label add-ons enforce “may a vendor buy labels” in their own controllers only. The queue applies the same rule before every create it does for a vendor (vendor panel click; cron for items of a vendor’s own rule): KavioConnector::vendorMayCreate($company_id) calls the label add-on’s Access::canLabel($company_id) (UPS, FedEx, Canada Post) or Access::canShip() (PostNord, DHL Express, DPD, GLS) with the vendor as runtime company. Sendcloud checks the vendor’s contract inside its createLabel(); InPost and Packeta have no such rule. Refused items move to Failed with the reason; vendor-forbidden carriers are hidden in the vendor’s rule editor and “Assign carrier”.

Implement Tygh\Addons\KvCarrierSelectionRules\Adapters\CarrierAdapter and register it with the PHP hook kv_carrier_selection_rules_adapters:

func.php
// init.php of your add-on
fn_register_hooks('kv_carrier_selection_rules_adapters');
function fn_my_addon_kv_carrier_selection_rules_adapters(&$adapters)
{
$adapters[] = new \Tygh\Addons\MyAddon\CsrAdapter(); // an object or a class name
}
namespace Tygh\Addons\MyAddon;
use Tygh\Addons\KvCarrierSelectionRules\Adapters\CarrierAdapter;
class CsrAdapter implements CarrierAdapter
{
public function code() { return 'my_freight'; } // a-z, 0-9, _ (max 64)
public function name() { return 'My Freight'; }
public function addonId() { return 'my_addon'; }
public function state() { return self::READY; } // READY | INACTIVE | MISSING
public function services() { return ['STD' => 'Standard']; } // [] = your add-on decides
public function capabilities()
{
return ['service' => true, 'parcels' => 1, 'weight' => false, 'note' => ''];
}
public function createLabels($order_id, array $preset)
{
// $preset: service, parcels, weight_kg (0 = weigh yourself), length, width, height (cm, 0 = unknown)
// Must be idempotent per order. Throw an exception with a readable message on failure.
return ['refs' => [/* your shipment ids */], 'tracking' => [/* tracking numbers */]];
}
public function labelFiles($order_id, array $refs)
{
// [[binary, mime type, file name], ...]; [] = not available yet (the queue asks again later)
return [];
}
}

The interface is guarded with interface_exists() on your side if your add-on must also run without this one.

Two optional methods (duck-typed, not part of the interface) make a third-party adapter as safe as the built-in ones:

  • vendorMayCreate($company_id): false when your add-on does not let this Multi-Vendor vendor have labels created (checked before every create done for a vendor). Without it your createLabels() must refuse itself.
  • liveRows($order_id): your shipment rows of the order that may hold a bought label (anything but cancelled). Without it the queue cannot see your shipments when an order is moved to another carrier.
  • change_order_status_post: routes an order when it reaches a trigger status (never changes the order).
  • delete_order: removes the queue item of a deleted order.
  • kv_sendcloud_label_created (Sendcloud 1.2+): a label made on the Sendcloud order tab or by its automatic label is taken over by the waiting queue item.
  • kv_sendcloud_label_canceled (Sendcloud 1.2+): a queue item holding a cancelled Sendcloud label moves to Failed with a note.
  • kv_carrier_selection_rules_adapters(&$adapters): list of adapters (objects or class names) before validation.
Table Content
?:kv_csr_rules rules: company, position, status, conditions (JSON), carrier, service, profile, parcels, mode (Q queue / A automatic), match counter
?:kv_csr_profiles parcel profiles: company, size (cm), packaging weight and limit (kg)
?:kv_csr_queue one row per order (UNIQUE order_id): rule, carrier, service, profile, parcels, mode, state, shipment refs of the label add-on, tracking, label count, error, attempts, batch
?:kv_csr_batches printed batches: company, user, items, labels, format, time. The print file is rebuilt from the labels the label add-ons store, so nothing large is kept here.

Queue states: N needs a carrier, Q queued, C creating, W waiting for the label file, L label ready, F failed, P printed, S removed.

Uninstalling keeps these tables (retention) unless Delete all data on uninstall is ticked.

Bundle::build() joins the label files of a batch: PDF labels are merged by Pdf\Merger (pure PHP: reads objects, compressed object streams of PDF 1.5+ and inherited page attributes; writes PDF 1.7 with a classic cross-reference table); ZPL / EPL labels are joined into one printer file; mixed formats and PDFs that cannot be merged (encrypted, damaged) go into one ZIP. No external library or service is used.

  • Console: php admin.php --dispatch=kv_carrier_selection_rules.cron (prints a JSON summary).
  • URL: index.php?dispatch=kv_carrier_selection_rules.cron&key=<cron key> (404 with a wrong key).

A run releases stale claims, fetches label files that were not ready yet and creates the labels of items in automatic mode (setting Labels per cron run).

  • Multi-Vendor: vendor panel access through schemas/permissions/vendor_multivendor.post.php (only when the setting allows it; cron never). Every list and action is scoped to the vendor’s company; marketplace rules (company 0) are read-only for vendors; parent orders are never queued.
  • Ultimate: schemas/permissions/vendor_ultimate.post.php closes the cron and licence check to store administrators; every object is compared with the runtime company.

The add-on has no storefront output; only the cron URL is a storefront dispatch (not cacheable, no session).