facetwp woocommerce _product_attributes

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php
/** some types of product attributes for woocommerce (those that are not taxonomies or used for variations
** are saved only in the _product_attributes custom field as an array
** select “_product_attributes” as the datasource in the facet and use facetwp_index_row to find and index
** the correct attributes
**/

add_filter( ‘facetwp_index_row’, function( $params, $class ) {
if ( ‘attributes’ == $params[‘facet_name’] ) { //change ‘attributes’ to name of your facet
$values = maybe_unserialize( $params[‘facet_value’] );
if ( is_array( $values ) && ! empty( $values ) ) {
foreach ( $values as $key => $value ) {
if ( ‘test-attr’ == $key ) { // Name of attribute, lowercase, “-” in place of spaces, “Test Attr” = ‘test-attr’
$attrs = explode( ‘|’, $value[‘value’] );
foreach ( $attrs as $attr ) {
$params[‘facet_value’] = trim( $attr );
$params[‘facet_display_value’] = $params[‘facet_value’];
$class->insert( $params ); // insert new value to the database
}
}
}
}
$params[‘facet_value’] = ”; // skip original row
}
return $params;
}, 10, 2 );
{{/gist}}