/** * The one parcel sent for an order: quotes, the Book check and the booking all use it. * Weight: the parcel entered in Packaging, else the product weights, where each product without * one counts as the default weight per unit (as in the Shopify app). * Size: the parcel entered in Packaging, else the default package from the linked Parcel2Go * account, else 30×20×15 cm, whatever the items (as in the Shopify app). Product sizes are not used. * Matches ParcelBuilder::build_parcel (includes/Helpers/ParcelBuilder.php), so the first quotes match the booking. * All values are in the store's units. */ import type { Order, ParcelDimensions, PluginSettings } from '../../types'; import { parseNum } from './parse'; import { cmToStoreUnit, kgToStoreUnit } from './unitBounds'; /** Parcel size when there are no product sizes and no default package: a typical small box. */ export const FALLBACK_PARCEL_CM = { length: 30, width: 20, height: 15 }; const FALLBACK_WEIGHT_KG = 1; export type ParcelWeightSource = 'packaging' | 'products' | 'default'; export interface ResolvedParcel extends ParcelDimensions { /** 'default' when at least one product has no weight and the default weight stands in for it. */ weightSource: ParcelWeightSource; /** Names of the order items whose product has no weight. */ missingWeightItems: string[]; /** Weight per unit used for each product without one. */ defaultWeight: number; } type ParcelSettings = Pick; /** * Items that go in the parcel: all but virtual or downloadable-only products (needs_shipping * false), which checkout leaves out of the cart's parcel too. Items without the flag count. * Matches ParcelBuilder::shipping_items. */ export function shippingItems(order: Order): Order['items'] { return (order.items ?? []).filter((item) => item.needs_shipping !== false); } function roundWeight(value: number): number { // Enough to hide float noise without a second visible rounding when displayed to 2 places. return Number(value.toFixed(6)); } function defaultItemWeight(order: Order, settings: ParcelSettings | null): number { const configured = settings?.overrides?.defaultWeightKg ?? 0; return configured > 0 ? configured : kgToStoreUnit(FALLBACK_WEIGHT_KG, order.units?.weight ?? 'kg'); } /** * Weight per unit of each order item: its product weight, or the default weight when it has none. * Used for the customs contents; an entered parcel weight does not change them. */ export function itemUnitWeights(order: Order, settings: ParcelSettings | null): number[] { const fallback = defaultItemWeight(order, settings); return (order.items ?? []).map((item) => { const weight = parseNum(item.dimensions?.weight) ?? 0; return weight > 0 ? weight : fallback; }); } export function resolveParcel(order: Order, settings: ParcelSettings | null): ResolvedParcel { const dimensionUnit = order.units?.dimension ?? 'cm'; const defaultWeight = defaultItemWeight(order, settings); const unitWeights = itemUnitWeights(order, settings); let productWeight = 0; const missingWeightItems: string[] = []; (order.items ?? []).forEach((item, index) => { // unitWeights lines up with order.items (customs uses every line); only shipped items weigh. if (item.needs_shipping === false) return; productWeight += unitWeights[index] * Math.max(0, item.quantity); if (!((parseNum(item.dimensions?.weight) ?? 0) > 0)) { missingWeightItems.push(item.name); } }); const defaultPackage = settings?.packages?.find((pkg) => pkg.id === settings.defaultPackageId); // Each side: entered in Packaging, else the default package, else 30×20×15 cm, as // SettingsController::get_default_parcel_dimensions does in PHP. const entered = order.parcel; const dimension = ( enteredValue: number | undefined, packageValue: number | undefined, fallbackCm: number ) => enteredValue && enteredValue > 0 ? enteredValue : packageValue && packageValue > 0 ? packageValue : cmToStoreUnit(fallbackCm, dimensionUnit); let weight: number; let weightSource: ParcelWeightSource; if (entered && entered.weight > 0) { weight = entered.weight; weightSource = 'packaging'; } else if (productWeight > 0) { weight = roundWeight(productWeight); weightSource = missingWeightItems.length > 0 ? 'default' : 'products'; } else { // No items, or every quantity is 0. weight = defaultWeight; weightSource = 'default'; } return { length: dimension(entered?.length, defaultPackage?.length, FALLBACK_PARCEL_CM.length), width: dimension(entered?.width, defaultPackage?.width, FALLBACK_PARCEL_CM.width), height: dimension(entered?.height, defaultPackage?.height, FALLBACK_PARCEL_CM.height), weight, weightSource, missingWeightItems, defaultWeight, }; }