Fieldline documentation1.x
Developers

Filters and hooks

Read answers in code, change a field in either checkout, check answers, and the other hooks add-ons use.

Fieldline'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 Fieldline's shortcut:

use Extendiment\Fieldline\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\Fieldline\Display::format( $field, $value ) turns a value into what customers read (an option's label, Yes/No), with Extendiment\Fieldline\Fields::get( 'gift_message' ) as the field.

fieldline_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( 'fieldline_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.

fieldline_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( 'fieldline_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', 'fieldline-field' );
	}
	return $args;
}, 10, 3 );

fieldline_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( 'fieldline_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 );

fieldline_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( 'fieldline_display_value', function ( $text, $field, $value ) {
	return 'vat_number' === $field['key'] ? strtoupper( $text ) : $text;
}, 10, 3 );

fieldline_loaded

An action that fires once Fieldline has loaded (on plugins_loaded, and only when WooCommerce is active). Hook an add-on's setup to it, so it only runs when Fieldline is there. FIELDLINE_VERSION is defined from then on, for version checks.

add_action( 'fieldline_loaded', function () {
	// Fieldline's classes, such as Extendiment\Fieldline\Fields, are available here.
} );

Hooks for add-ons

These are what Fieldline Pro is built on, and what you'd use to add a field type or settings of your own:

Hook What it's for
fieldline_field_types Register a field type. See Adding a field type.
fieldline_clean_field Clean and keep a setting of your own on a field ($clean, $raw). Fieldline's own settings in the result are ignored.
fieldline_handles_conditions Return true if your code checks the conditions setting on fields. Without it, Fieldline leaves fields with conditions out of checkout.
fieldline_classic_shows Whether a field with conditions shows while a classic checkout order is placed ($shows, $field, $group, $posted).
fieldline_field_rows Print rows in a field's settings table ($value). A row with class fieldline-for and data-types="text textarea" only shows for those types.
fieldline_field_sections Print a section after the field's settings table, inside its form.
fieldline_posted_field Add your settings from $_POST when a field is saved (the nonce and capability are already checked).
fieldline_field_error Return a message code to refuse a field's settings; add its text with fieldline_admin_messages.
fieldline_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.