Filters and hooks
Read answers in code, change a field in either checkout, check answers, and the other hooks add-ons use.
Checkoutline's hooks are WordPress filters and actions. Put the examples in a small custom plugin or your child theme's functions.php.
Reading an answer
Answers are WooCommerce additional checkout field values, so read them with WooCommerce's own API, or Checkoutline's shortcut:
use Extendiment\Checkoutline\Checkout;
// A contact or order field.
$gift = Checkout::value( $order, 'gift_message' );
// An address field, from the shipping address.
$gate = Checkout::value( $order, 'gate_code', 'shipping' );
Checkboxes come back as true or false, everything else as the saved string ('' when there's no answer). Extendiment\Checkoutline\Display::format( $field, $value ) turns a value into what customers read (an option's label, Yes/No), with Extendiment\Checkoutline\Fields::get( 'gift_message' ) as the field.
checkoutline_block_field
The arguments a field is registered with for the Checkout block, through WooCommerce's woocommerce_register_additional_checkout_field(). Gets the arguments and the field.
// Let the browser fill in a customer number.
add_filter( 'checkoutline_block_field', function ( $args, $field ) {
if ( 'customer_number' === $field['key'] ) {
$args['attributes'] = array( 'autocomplete' => 'on', 'maxLength' => 12 );
}
return $args;
}, 10, 2 );
WooCommerce only accepts certain attributes on text fields; see its documentation for additional checkout fields.
checkoutline_classic_field
The same field in the classic checkout, as woocommerce_form_field() arguments. Gets the arguments, the field, and the group it's in (billing, shipping or order).
// Half-width, next to each other.
add_filter( 'checkoutline_classic_field', function ( $args, $field, $group ) {
if ( in_array( $field['key'], array( 'floor', 'door' ), true ) ) {
$args['class'] = array( 'door' === $field['key'] ? 'form-row-last' : 'form-row-first', 'checkoutline-field' );
}
return $args;
}, 10, 3 );
checkoutline_value_problem
Checks a customer's answer before the order is placed, in both checkouts. Return a message to refuse it, or '' to accept it. Gets the problem so far, the field, the answer (trimmed, never empty: empty answers are WooCommerce's required check) and which checkout asks (block or classic).
// Gate codes are four digits.
add_filter( 'checkoutline_value_problem', function ( $problem, $field, $value, $checkout ) {
if ( '' === $problem && 'gate_code' === $field['key'] && ! preg_match( '/^\d{4}$/', $value ) ) {
return __( 'The gate code is four digits.', 'my-plugin' );
}
return $problem;
}, 10, 4 );
checkoutline_display_value
An answer as it's shown on the order screen, the order confirmation, My Account and in emails, as text. Gets the text, the field and the saved value.
add_filter( 'checkoutline_display_value', function ( $text, $field, $value ) {
return 'vat_number' === $field['key'] ? strtoupper( $text ) : $text;
}, 10, 3 );
checkoutline_loaded
An action that fires once Checkoutline has loaded (on plugins_loaded, and only when WooCommerce is active). Hook an add-on's setup to it, so it only runs when Checkoutline is there. CHECKOUTLINE_VERSION is defined from then on, for version checks.
add_action( 'checkoutline_loaded', function () {
// Checkoutline's classes, such as Extendiment\Checkoutline\Fields, are available here.
} );
Hooks for add-ons
These are what Checkoutline Pro is built on, and what you'd use to add a field type or settings of your own:
| Hook | What it's for |
|---|---|
checkoutline_field_types |
Register a field type. See Adding a field type. |
checkoutline_clean_field |
Clean and keep a setting of your own on a field ($clean, $raw). Checkoutline's own settings in the result are ignored. |
checkoutline_handles_conditions |
Return true if your code checks the conditions setting on fields. Without it, Checkoutline leaves fields with conditions out of checkout. |
checkoutline_classic_shows |
Whether a field with conditions shows while a classic checkout order is placed ($shows, $field, $group, $posted). |
checkoutline_field_rows |
Print rows in a field's settings table ($value). A row with class checkoutline-for and data-types="text textarea" only shows for those types. |
checkoutline_field_sections |
Print a section after the field's settings table, inside its form. |
checkoutline_posted_field |
Add your settings from $_POST when a field is saved (the nonce and capability are already checked). |
checkoutline_field_error |
Return a message code to refuse a field's settings; add its text with checkoutline_admin_messages. |
checkoutline_field_summary |
Lines of plain text under a field's name in the list. |
The code documents each hook's arguments where it's applied.