Custom filter sources

When creating filters in the builder, the first step is to choose the data source. By default, FiboFilters add basic WooCommerce data to the source list, such as:

  • categories
  • tags
  • attributes
  • price
  • stock status
  • ‘On sale’ information

Plugin integrations can add new records to the source list. For example, the ACF integration adds custom fields related to products as a new data source. Another example is the ‘Brands’ value added to the sources.

However, some sources, such as taxonomies or custom fields (aside from those mentioned before) can’t be added automatically. To add them to the list, use these PHP snippets.

FiboFilters 1.10.0 introduces also a powerful feature called computed filters which allows you to filter products by virtually anything. Scroll down to “Computed filters” to read more about it.

Table of Contents

Support for custom taxonomies

Let’s assume that a new taxonomy, “Manufacturer”, has been registered. For this taxonomy to be added to the “Data source” list, you need to use this PHP snippet:

add_filter(
    'fibofilters/filters/custom_sources/taxonomies',
    function ( $taxonomies ) {
        $taxonomies[] = [
            'label'    => 'Manufacturer',
            'taxonomy' => 'manufacturer',
        ];

        return $taxonomies;
    }
);

Learn how to add this snippet to your WordPress.

If the snippet is inserted correctly, a new position will be added to the “Data source” list: “Manufacturer”.

Custom taxonomy Manufacturer
Custom taxonomy “Manufacturer” is listed on the filter data source dropdown.

Support for custom fields

Every custom field can be set as a filter’s source data. However, these fields must be first registered – this process is not done automatically.  Our integrations with third-party plugins, such as Advanced Custom Fields, are the exception. In this case, custom fields will be immediately selectable as a data source.

To make any custom field selectable from the Data source dropdown, you need to register them with this PHP snippet. Let’s assume that two custom fields were created – “Feature” and “Scale”. Their values are saved in the postmeta table, where the meta_key s are feature and scale, respectively.

add_filter(
	'fibofilters/filters/custom_sources/custom_fields',
	function ( $custom_fields ) {

		$custom_fields[] = [
			'field_slug'  => 'feature', // meta_key
			'label'       => 'Custom field: Feature',
			'label_front' => 'Product Feature',
		];

		$custom_fields[] = [
			'field_slug'  => 'scale', // meta_key
			'label'       => 'Custom field: Scale',
			'label_front' => 'Product Scale',
		];

		return $custom_fields;
	}
);

Learn how to add this snippet to your WordPress.

If the snippet is inserted correctly, two new positions will be added to the “Data source” list: “Custom field: Feature” and “Custom field: Scale”.

Custom fields on the data source list
Custom fields “Feature” and “Scale” are listed on the filter data source dropdown.

Excluding terms from the product category filter

Starting with FiboFilters 1.10.0, you can now remove specific terms from the product_cat (product category) filter. The following example excludes the “Accessories” category term (ID = 205), moving its children (“Backpacks”, “Belts” and “Wallets”) to the top of the category filter tree:

add_filter( 'fibofilters/indexer/filters_source_query/hidden_term_ids/source=tax_product_cat', function () {
	return [ 205 ]; // Accessories
} );

Learn how to add this snippet to your WordPress.

After adding the code, rebuild the index in WooCommerce > FiboFilters > Indexer.

Here’s a before and after:

FiboFilters: remove term from product category filter

Computed filters

FiboFilters 1.10.0 introduces a new feature called computed filters which allows you to define dynamic, code-driven filter sources – meaning you can filter products by virtually anything.

To create a computed filter, follow these three steps:

  1. Register the computed data source via code.
  2. Create a filter in the filter builder.
  3. Rebuild the filter index.

The following example will show you how to create a checkbox filter with a random number (1–5) for each product.

Step 1: Register the custom data source via code.

To register the computed data source, add the following code to your website:

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Custom source (backend label)', // Backend label in filter builder
		'label_front' => 'Custom source (frontend label)', // Front-end label
		'computed'    => 'random-checkboxes', // Unique identifier
		'type'        => 'checkboxes', // Filter type
		'callback'    => function ( $product_id, $parent_id ) {
			return 'Checkbox ' . wp_rand( 1, 5 );
		},
	];

	return $sources;
} );

Learn how to add this snippet to your WordPress.

Parameters:

  • label: backend label (shown in filter builder)
  • label_front: front-end display label
  • computed: unique filter identifier
  • type: checkboxes, buttons, radio, range, toggle, select or color
  • callback: a callback function that returns filter values for each product. This function is called by FiboFilters during indexing. Any change to this function requires an index rebuild.

Callback parameters:

  • $product_id: ID of product being indexed
  • $parent_id: parent product ID (for variable products) or null for simple products

Callback return formats vary by filter type:

  • select / checkboxes:
    • scalar (a single value of any type), eg. 'blue', 1
    • array of scalars, eg. ['blue', 'green'], [1, 2, 3]
    • hierarchical array (must include the whole hierarchy and not only a part of it), eg.
[
	[
		'value'        => '1',
		'label'        => 'Value 1',
		'label_url'    => 'value-1',
		'parent_value' => '0',
	],
	[
		'value'        => '2',
		'label'        => 'Value 2',
		'label_url'    => 'value-2',
		'parent_value' => '1',
	],
	[
		'value'        => '3',
		'label'        => 'Value 3',
		'label_url'    => 'value-3',
		'parent_value' => '2',
	],
]
  • buttons / radio:
    • scalar
    • array of scalars
  • range: numeric value (eg. 1, 1.5). The numeric value can be also written as a string, eg. '1', '1.5'
  • toggle: an array where the first value is a boolean positive value ('1', 'yes', 'true', 'on') and the second value is a label, eg. [ '1' , 'In stock' ], [ 'yes' , 'In stock' ]
  • color:
    • string, eg. 'blue'
    • array of strings, eg. ['Red', 'Green'] or [ '#ff0000', '#00ff00', '#0000ff' ]
    • an array of arrays where the first element is a color and the second a label, eg.
      [ [ '#ff0000', 'Red'] , [ '#00ff00', 'Green' ], [ '#0000ff', 'Blue' ] ]
      or
      [ [ '#ff0000', 'Red'] ]

Step 2: Create a filter in the filter builder.

The next step is to create a filter from the computed data source. Go to WooCommerce > FiboFilters > Filters, click “New Filter +” and select the newly-created source from the dropdown:

Step 3: Rebuild the index in the “Indexer” tab.

Once these three steps are completed, you will see the new filter in the front-end:

FiboFilters: computed filters on the front-end

Examples

Stock status toggle based on two custom fields

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Custom stock status',
		'label_front' => 'Custom stock status',
		'computed'    => 'custom_stock_status',
		'type'        => 'toggle',
		'callback'    => function ( $product_id, $parent_id ) {
			$post_id = ! empty( $parent_id ) ? $parent_id : $product_id;
			$store1  = get_post_meta( $post_id, 'store1', true );
			$store2  = get_post_meta( $post_id, 'store2', true );

			return $store1 === '1' && $store2 === '1' ? [ '1', 'In Stock' ] : '';
		},
	];
	return $sources;
} );

Learn how to add this snippet to your WordPress.

Select with random values

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Select with random numbers',
		'label_front' => 'Select with random numbers',
		'computed'    => 'select',
		'type'        => 'select',
		'callback'    => function ( $product_id, $parent_id ) {
			return 'Select ' . wp_rand( 10, 15 );
		},
	];
	return $sources;
} );

Learn how to add this snippet to your WordPress.

Toggle with products whose names start with a vowel

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'The product name begins with a vowel',
		'label_front' => 'The product name begins with a vowel',
		'computed'    => 'vowel-start',
		'type'        => 'toggle',
		'callback'    => function ( $product_id, $parent_id ) {
			$product      = wc_get_product( ! empty( $parent_id ) ? $parent_id : $product_id );
			$title        = $product->get_title();
			$first_letter = mb_substr( $title, 0, 1 );
			if ( in_array( mb_strtolower( $first_letter ), [ 'a', 'e', 'i', 'o', 'u', 'y' ], true ) ) {
			return [ 'yes', 'Starts with a vowel' ];
			}

			return '';
		},
	];
	return $sources;
} );

Learn how to add this snippet to your WordPress.

Range with the product name length

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Name length',
		'label_front' => 'Name length',
		'computed'    => 'name-length',
		'type'        => 'range',
		'callback'    => function ( $product_id, $parent_id ) {
			$product = wc_get_product( ! empty( $parent_id ) ? $parent_id : $product_id );
			$title   = $product->get_title();

			return strlen( $title );
		},
	];
	return $sources;
} );

Learn how to add this snippet to your WordPress.

Radio with random values

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Radio with random numbers',
		'label_front' => 'Radio with random numbers',
		'computed'    => 'radio',
		'type'        => 'radio',
		'callback'    => function ( $product_id, $parent_id ) {
			return 'Radio ' . wp_rand( 1, 5 );
		},
	];
	return $sources;
} );

Learn how to add this snippet to your WordPress.

Buttons with random numbers

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Buttons with random numbers',
		'label_front' => 'Buttons with random numbers',
		'computed'    => 'buttons',
		'type'        => 'buttons',
		'callback'    => function ( $product_id, $parent_id ) {
		return 'Button ' . wp_rand( 1, 5 );
		},
	];
	return $sources;
} );

Learn how to add this snippet to your WordPress.

One filter from multiple custom fields

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Availability',
		'label_front' => 'Availability',
		'computed'    => 'availability-chbx',
		'type'        => 'checkboxes',
		'callback'    => function ( $product_id, $parent_id ) {
			$availability = array();
			if ( ff_get_post_meta( $product_id, '_is_cracow_available' ) ) {
				$availability[] = "Kraków";
			}
			if ( ff_get_post_meta( $product_id, '_is_warsaw_available' ) ) {
				$availability[] = "Warszawa";
			}
			if ( ff_get_post_meta( $product_id, '_is_gdansk_available' ) ) {
				$availability[] = "Gdańsk";
			}
			return $availability;
		},
	];
	return $sources;
} );

function ff_get_post_meta( $product_id, $meta_key ) {
	$meta_value = get_post_meta( $product_id, $meta_key, true );
	if ( ! empty( $meta_value ) && 1 == $meta_value ) {
		return true;
	}
	return false;
}

Learn how to add this snippet to your WordPress.

Parcel locker size

Creates a filter with parcel locker sizes (‘Size XS’, ‘Size A’, ‘Size B’, ‘Size C’ or ‘Out of scale’) calculated for each product based on its dimensions and weight.

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Parcel Locker shipment',
		'label_front' => 'Parcel Locker shipment',
		'computed'    => 'checkboxes',
		'type'        => 'checkboxes',
		'callback'    => function ( $product_id ) {

			$parcel_locker_shipment = [
				'Size XS (4 × 23 × 40 cm)' => [ 4, 23, 40, 3 ],  // height, width, length, weight.
				'Size A (8 × 38 × 64 cm)'  => [ 8, 38, 64, 25 ],
				'Size B (19 × 38 × 64 cm)' => [ 19, 38, 64, 25 ],
				'Size C (41 × 38 × 64 cm)' => [ 41, 38, 64, 25 ],
			];

			$product      = wc_get_product( $product_id );
			$product_size = [
				$product->get_height(),
				$product->get_width(),
				$product->get_length(),
				$product->get_weight(),
			];

			foreach ( $parcel_locker_shipment as $label => $matrix ) {
				$fit = true;
				foreach ( $matrix as $i => $value ) {
					if ( $product_size[ $i ] >= $value ) {
						$fit = false;
						break;
					}
				}
				if ( $fit ) {
					return $label;
				}
			}

			return 'Out of scale';
		},
	];

	return $sources;
} );

Learn how to add this snippet to your WordPress.

Custom attributes

Creates a filter from custom attributes defined in the product editor. In this example, the attributes are stored as a string with values separated with a dash (-), eg. AA 11 - BB 22 - CC 33.

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Motortype (custom)',
		'label_front' => 'Motortype',
		'computed'    => 'motortype-checkboxes',
		'type'        => 'checkboxes',
		'callback'    => function ( $product_id, $parent_id ) {
			$product = wc_get_product( $product_id );
			$attr = $product->get_attribute( $attribute );
			$attr = explode( ' - ', $attr ); 
			$attr = array_unique( $attr );
			return $attr;
		},
	];

	return $sources;
} );

Learn how to add this snippet to your WordPress.

Support for hierarchical taxonomies

FiboFilters 1.10.0 introduces two helper hooks that make it easy to create custom hierarchical filters:

  • fibofilters/indexer/helper/get_ancestor_tree_as_values: returns ancestor terms for a given term
  • fibofilters/indexer/helper/get_children_values: returns child terms for a given term

Using these two hooks, you can create a filter based on a hierarchical taxonomy (like product categories), removing some terms and keeping the hierarchy.

Let’s assume that your product category tree looks like this:

  • Accessories
    • Backpacks
    • Belts
      • Green belts
      • Red belts
    • Wallets
  • Caps
  • T-shirts
  • Trousers

Now you want to create a filter that consists only of children of the “Accessories” category. You can achieve it with the following code:

add_filter( 'fibofilters/filters/custom_sources/computed', function ( $sources ) {
	$sources[] = [
		'label'       => 'Checkboxes - Accessories',
		'label_front' => 'Checkboxes - Accessories',
		'computed'    => 'checkboxes-accessories',
		'type'        => 'checkboxes',
		'callback'    => function ( $product_id, $parent_id ) {
			$object_id    = ! empty( $parent_id ) ? $parent_id : $product_id;
			$object_terms = wp_get_object_terms( $object_id, 'product_cat' );
			// Get tree of ancestor categories for the product (including the product's category).
			$values = apply_filters( 'fibofilters/indexer/helper/get_ancestor_tree_as_values', [], $object_terms );
			// Get only children of Accessories category (ID: 205).
			return apply_filters( 'fibofilters/indexer/helper/get_children_values', $values, 205 );
		},
	];

	return $sources;
} );

Learn how to add this snippet to your WordPress.

This is what it looks like on the front-end:

FiboFilters: hierarchical filter based on part of product categories