Skip to content

Instantly share code, notes, and snippets.

@djrmom
Created November 15, 2018 00:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djrmom/a8e4380a51ae0a95b00e8383a2a99629 to your computer and use it in GitHub Desktop.
Save djrmom/a8e4380a51ae0a95b00e8383a2a99629 to your computer and use it in GitHub Desktop.
facetwp woocommerce _product_attributes
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment