Developer notes
Add-on identity
Section titled “Add-on identity”- Add-on id:
kv_multi_parcel, namespaceTygh\Addons\KvMultiParcel. - PHP 7.4 syntax floor, runs on PHP 7.2.5 to 8.3 depending on the CS-Cart version. Smarty 4 and 5.
- Editions: CS-Cart, CS-Cart Ultimate, Multi-Vendor, Multi-Vendor Ultimate.
Structure
Section titled “Structure”app/addons/kv_multi_parcel/ addon.xml, init.php, func.php controllers/backend/kv_multi_parcel.php boxes, parcel actions, packing slips controllers/backend/orders.post.php data of the Parcels tab controllers/backend/kv_multi_parcel_license.php schemas/menu, schemas/permissions (admin + vendor_multivendor) Tygh/Addons/KvMultiParcel/ Config, Access, Boxes, Packer, Parcels, Plan, Shipping, Labels, Adapters Adapters/CarrierAdapter.php the interface for carrier connectors License/, Upgrade/ licence client and self-healing upgradesdesign/backend/templates/addons/kv_multi_parcel/ order tab, boxes page, packing slip, licence cardvar/themes_repository/responsive/templates/addons/kv_multi_parcel/ customer order page linesDatabase tables
Section titled “Database tables”kv_multi_parcel_boxes: the catalogue.company_id0 = the store owner (shared), otherwise the vendor or storefront.kv_multi_parcel_parcels: one row per parcel.order_id,parcel_no(unique per order), box snapshot, weight, dimensions, carrier, tracking number and link, adapter state (stateP planned, Q label requested, T tracking number known),request_key,label_ref,core_shipment_id.kv_multi_parcel_items: quantity of each order item in each parcel.
Core shipment
Section titled “Core shipment”CS-Cart needs unshipped products for every shipment, so N parcels cannot be N core shipments. The add-on creates one core shipment with fn_update_shipment() and puts every number into it: tracking_number = “A, B, C” (255 characters), comments = the parcels with carrier, number and link, separated by “ | “ (the core e-mail and shipment tab print comments on one line). Shipping again calls fn_update_shipment($data, $shipment_id) on the same shipment. The storefront hooks orders:shipment_info and orders:totals_shipping list every parcel.
Hooks used
Section titled “Hooks used”get_order_info, delete_order, delete_shipments, delete_company (signatures identical in 4.17.2 and 4.21.2).
Carrier adapter: create N labels from your connector
Section titled “Carrier adapter: create N labels from your connector”A carrier add-on registers an adapter and the Parcels tab offers a Create labels button for it. The add-on never modifies other add-ons; connectors opt in.
- Implement
Tygh\Addons\KvMultiParcel\Adapters\CarrierAdapter:
class DpdParcelAdapter implements \Tygh\Addons\KvMultiParcel\Adapters\CarrierAdapter{ public function code() { return 'dpd'; } // [a-z0-9_]{1,64} public function name() { return 'DPD'; } // button label and parcel carrier public function supports(array $order) { return /* country, shipping method, account ... */ true; } public function maxParcels() { return 20; } public function createLabels(array $order, array $parcels, array $context) { /* see below */ } public function cancelLabels(array $order, array $parcels) { return true; }}- Register it from the connector’s
init.php(fn_register_hooks('kv_multi_parcel_adapters')) and answer the hook infunc.php:
function fn_my_connector_kv_multi_parcel_adapters(&$adapters){ $adapters['dpd'] = \Tygh\Addons\MyConnector\DpdParcelAdapter::class; // class name or object}createLabels()receives the order (fn_get_order_info()), the parcels without a tracking number and a context. Each parcel hasparcel_id,parcel_no,parcel_count,request_key,weight,length,width,height,box_name,noteanditems(item_id,product_id,name,code,qty,weight). Returnparcel_no => ['tracking_number' => ..., 'tracking_url' => ..., 'label_ref' => ..., 'label_format' => 'pdf']. A parcel without a tracking number in the answer stays open and the administrator sees a partial result.
Rules the add-on enforces and your adapter must follow:
- Idempotency.
request_keyis stored before your call and is the same on every retry. Send it to the carrier as the reference or idempotency key. When$context['retry']is true an earlier call ended without an answer (timeout, crash): look the parcels up at the carrier byrequest_keyand return what exists, do not create a second label. - One lock per order. Calls are serialised with a database lock. Do not call
createLabels()yourself; useLabels::create($order_id, $code). - Failures. Throw an exception. The message (cut to 255 characters) is shown to the administrator and the parcels stay “label requested”.
- Carriers without a master shipment id may create N independent labels in a loop; use
maxParcels()to limit them. - Labels are not stored by this add-on. Keep your own label files;
label_refis your key to find them.
Optional: lookupLabels(array $order, array $parcels) (same answer shape as createLabels(), look up by request_key). The Reset label request button on parcels whose request got no answer calls it first and adopts labels that exist; otherwise the parcels go back to planned (the request key is kept) so the merchant can change weight, box or items and request again.
After labels exist the parcels (items, boxes, count) are locked. Labels::cancel($order_id) calls cancelLabels() and returns the parcels to “planned”.
Hook after the labels were stored: fn_set_hook('kv_multi_parcel_labels_created', $order_id, $adapter_code, $parcel_numbers).
Packing engine
Section titled “Packing engine”Packer::suggest($lines, $boxes, $fill_percent) is pure PHP and can be called from other code. Lines are item_id, qty, weight, length, width, height; boxes are rows of the box table. It returns parcels (box, weight, dimensions, items per parcel) and the number of unboxed parcels.
php81 tests/unit.phpandphp83 tests/unit.php: pure logic, no store needed.tests/php/store_test.php,tests/e2e/e2e.mjs,tests/php/lifecycle.php: run on a dev store (seetests/test-plan.md). Mail never leaves the process in these tests, and no carrier is contacted.
License
Section titled “License”The add-on checks its license once a day (domain name only) and gates new work with Config::active(). See the license card on the add-on page.