ShippingFormTrait
Step 2: shipping and payment on one screen.
The payment price list is a matrix of tariff, method and currency, so a payment price cannot be computed without a selected tariff; splitting it into two steps would force going back.
IMPORTANT: the items of both radio lists (and of the pickup branch select) are built from the preview computed for the tariff from POST data (Checkout::getSelectedTariffId()), not from the session; otherwise the submitted payment would not be among the items and Nette would discard it.
A method with available = 0 is not offered as selectable: the server rejects it anyway
(buildPaymentMethod() calls allowUnavailable = false on the front end), so offering it
would be a promise checkout cannot keep.
Table of Contents
Methods
- onShippingSubmit() : void
- Handles AJAX auto-update: stores the unfinished choice, drops the preview and redraws the snippet.
- onShippingSuccess() : void
- createComponentShippingForm() : Form
- branchItems() : array<int, string>
- Returns branches (`shipper_tariff_branches`) of the selected tariff.
- decodePickupPoint() : array<string, mixed>|null
- Decodes the raw JSON from the `pickupPoint` hidden field into an associative array.
- isPickupPointMissing() : bool
- Returns whether the selected tariff requires a pickup point that is missing in the unfinished checkout.
- offeredId() : int|null
- Returns the id when it is among the items, otherwise null.
- optionInt() : int
- optionRows() : array<int, array<string, mixed>>
- Returns offer rows from the Api response.
- optionStr() : string
- persistShippingSelection() : void
-
pickupInfo()
: array{requiresPickup: bool, widget: array
|null}|null - Returns the pickup point info of the selected tariff.
- pickupPointJson() : string
- Encodes the point back for the hidden field `setDefaults()`; the exact opposite of {@see decodePickupPoint()}.
-
shippingOptions()
: array{shipping: list
>, payment: list >} - stringKeyed() : array<string, mixed>
- Narrows `array<mixed, mixed>` to `array<string, mixed>`.
Methods
onShippingSubmit()
Handles AJAX auto-update: stores the unfinished choice, drops the preview and redraws the snippet.
public
onShippingSubmit(Form $form) : void
Runs before validation, so an incomplete choice (shipping only) breaks nothing.
Parameters
- $form : Form
onShippingSuccess()
public
onShippingSuccess(Form $form) : void
Parameters
- $form : Form
createComponentShippingForm()
protected
createComponentShippingForm() : Form
Return values
FormbranchItems()
Returns branches (`shipper_tariff_branches`) of the selected tariff.
private
branchItems(array<int, array<string, mixed>> $shipping, int|null $tariffId) : array<int, string>
A tariff without branches yields an empty array, so the select is not rendered and not required.
Parameters
- $shipping : array<int, array<string, mixed>>
- $tariffId : int|null
Return values
array<int, string>decodePickupPoint()
Decodes the raw JSON from the `pickupPoint` hidden field into an associative array.
private
decodePickupPoint(string|null $raw) : array<string, mixed>|null
Invalid JSON, an empty string or anything other than a JSON object yields null
(nothing selected; the page must never crash).
Parameters
- $raw : string|null
Return values
array<string, mixed>|nullisPickupPointMissing()
Returns whether the selected tariff requires a pickup point that is missing in the unfinished checkout.
private
isPickupPointMissing() : bool
The offer is read from a fresh preview (after persistShippingSelection()), so it applies to
the tariff just submitted, not to the one from the previous request.
Return values
boolofferedId()
Returns the id when it is among the items, otherwise null.
private
offeredId(int|null $id, array<int, string> $items) : int|null
Parameters
- $id : int|null
- $items : array<int, string>
Return values
int|nulloptionInt()
private
optionInt(mixed $value) : int
Parameters
- $value : mixed
Return values
intoptionRows()
Returns offer rows from the Api response.
private
optionRows(mixed $rows) : array<int, array<string, mixed>>
Values are mixed, so they are normalized to array<string, mixed> and invalid ones are
dropped (the form must not receive half of the data).
Parameters
- $rows : mixed
Return values
array<int, array<string, mixed>>optionStr()
private
optionStr(mixed $value) : string
Parameters
- $value : mixed
Return values
stringpersistShippingSelection()
private
persistShippingSelection(Form $form) : void
Parameters
- $form : Form
pickupInfo()
Returns the pickup point info of the selected tariff.
private
pickupInfo(array<int, array<string, mixed>> $shipping, int|null $tariffId) : array{requiresPickup: bool, widget: array|null}|null
null means the tariff either needs no pickup point or is not in the offer at all (a
defense against an invalid POST).
Parameters
- $shipping : array<int, array<string, mixed>>
- $tariffId : int|null
Return values
array{requiresPickup: bool, widget: arraypickupPointJson()
Encodes the point back for the hidden field `setDefaults()`; the exact opposite of {@see decodePickupPoint()}.
private
pickupPointJson(mixed $point) : string
Parameters
- $point : mixed
Return values
stringshippingOptions()
private
shippingOptions() : array{shipping: list>, payment: list>}
Return values
array{shipping: liststringKeyed()
Narrows `array<mixed, mixed>` to `array<string, mixed>`.
private
stringKeyed(array<string|int, mixed> $value) : array<string, mixed>
The Api sends an associative array with string keys, but PHPStan cannot infer that from
is_array() alone.
Parameters
- $value : array<string|int, mixed>