Swatchline documentation1.x
Developers

Filters and hooks

Read swatch data in code, change an attribute's type or swatches, add swatch types, and extend the product page script.

Swatchline's hooks are WordPress filters and actions. Put the examples in a small custom plugin or your child theme's functions.php. Swatchline Pro is built on these same hooks.

Where the data is

Data Stored in Read with
An attribute's swatch type The swatchline_attribute_types option (attribute id => type; no entry is Dropdown) Swatches::attribute_type( $id ), Swatches::taxonomy_type( 'pa_colour' )
A value's colour Term meta swatchline_color (#rrggbb) Swatches::term_color( $term_id )
A value's image Term meta swatchline_image (attachment id) Swatches::term_image( $term_id )
The settings swatchline_* options Settings::shape(), Settings::size()…

The classes are in the Extendiment\Swatchline namespace. Swatches::for_attribute( $product, 'pa_colour', $values ) returns what an attribute shows on a product: its type and each option's value, label, colors and image, or null for a dropdown.

swatchline_attribute_type

The type of an attribute on a product, before its swatches are built: select (dropdown), color, image, label or an add-on's type.

// Sizes are always buttons, whatever the attribute says.
add_filter( 'swatchline_attribute_type', function ( $type, $attribute, $product ) {
	return 'pa_size' === $attribute ? 'label' : $type;
}, 10, 3 );

swatchline_attribute_swatches

The swatches an attribute shows, or null for a dropdown. Change an option's label, its colors (one, or two for a split swatch) or its image (an attachment id).

add_filter( 'swatchline_attribute_swatches', function ( $swatches, $product, $attribute ) {
	if ( is_array( $swatches ) && 'pa_colour' === $attribute ) {
		foreach ( $swatches['options'] as $n => $option ) {
			if ( 'rainbow' === $option['value'] ) {
				$swatches['options'][ $n ]['colors'] = array( '#e11d48', '#2563eb' );
			}
		}
	}
	return $swatches;
}, 10, 3 );

swatchline_swatch_types

The types offered on the attribute screen. A new type must be turned into color, image or label in swatchline_attribute_swatches; any type Swatchline can't draw shows as a dropdown.

swatchline_variation and swatchline_load_variations

The product page gets a compact list of the product's variations (id, attributes, in_stock) to cross out what can't be bought. swatchline_variation filters each row; swatchline_load_variations returns false to leave the list out for a product.

add_filter( 'swatchline_load_variations', function ( $load, $product ) {
	return $product->get_id() !== 123 ? $load : false;
}, 10, 2 );

The product page

Hook Use
swatchline_frontend_config (filter) The script's settings, window.swatchlineConfig: showSelected, soldOut (cross), i18n
swatchline_swatches_html (filter) $html, $swatches, $selected, $args: a group's markup
swatchline_enqueue_scripts (action) Fires with Swatchline's styles and script

The script exposes window.swatchline: state( variations, attributes, chosen, name, value ) returns ok, out or none, and refresh( form ) brings a form's swatches up to date after you change its dropdowns in code (changing a dropdown and firing its change event does that too).

Admin screens

Hook Use
swatchline_term_fields (action) $context (add or edit), $taxonomy, $term: fields after Swatchline's on a term form. Admin\TermFields::row() and color_control() / image_control() draw them the same way
swatchline_save_term (action) $term_id, $taxonomy: after Swatchline saved a term's fields (nonce and capability checked)
swatchline_term_column (filter) $chips, $term_id: the Swatch column
swatchline_settings (filter) The fields of the Swatches settings section (WooCommerce settings API)

swatchline_loaded

Fires once Swatchline has loaded, on plugins_loaded, only when WooCommerce is active. Add-ons start there. SWATCHLINE_VERSION, SWATCHLINE_FILE, SWATCHLINE_URL and SWATCHLINE_DIR are defined.