Created
November 15, 2018 00:17
-
-
Save djrmom/a8e4380a51ae0a95b00e8383a2a99629 to your computer and use it in GitHub Desktop.
facetwp woocommerce _product_attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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