Gists

4 months ago
<?php
// output only sub categories of current woocommerce product category
add_filter( 'facetwp_facet_render_args', function( $args ) {

    if ( 'my_facet_name' == $args['facet']['name'] && is_product_category() ) {  // replace 'my_facet_name' with the name of your facet

        $current_term = get_queried_object_id();

        foreach ( $args['values'] as $key => $row ) {
            /* to also include current term in chocies add this to if:
               && $row['term_id'] != $current_term
            */
            if ( $row['parent_id'] != $current_term ) {
                unset( $args['values'][$key] );
            }
        }

        $args['values'] = array_values( $args['values'] ); // fixes indexes
    }

    return $args;
});
2 years ago
<?php

// index date as mmdd for example 1225
add_filter( 'facetwp_index_row', function( $params, $class ) {
	if ( 'today' == $params['facet_name'] ) { // change today to name of your facet
		$raw_value = $params['facet_value'];
		$params['facet_value'] = date( 'md', strtotime( $raw_value ) );
		$params['facet_display_value'] = date( 'F j', strtotime( $raw_value ) );
	}
	return $params;
}, 10, 2 );

// output only the facet choice for today with label of today, if
// there are none, this will be a ghost with show ghosts enabled
add_filter( 'facetwp_facet_render_args', function( $args ) {
    if ( 'today' == $args['facet']['name'] ) {

        $lookup = [];
        $temp_values = [];
        $days = [ date( 'md' ) ];

        foreach ( $args['values'] as $row ) {
            $lookup[ $row['facet_value'] ] = $row;
        }

        foreach ( $days as $day ) {
            if ( isset( $lookup[ $day ] ) ) {
                $temp_values[] = $lookup[ $day ];
            }
            else {
                $temp_values[] = [
                    'facet_value' => $day,
                    'facet_display_value' => 'Today',
                    'counter' => 0
                ];
            }
        }

        $args['values'] = $temp_values;
    }

    return $args;
});
6 months ago
<?php

add_filter( 'facetwp_facet_render_args', function( $args ) {
    if ( 'program_days' == $args['facet']['name'] ) {
        $lookup = [];
        $temp_values = [];
        $days = [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' ];

        foreach ( $args['values'] as $row ) {
            $lookup[ $row['facet_value'] ] = $row;
        }

        foreach ( $days as $day ) {
            if ( isset( $lookup[ $day ] ) ) {
                $temp_values[] = $lookup[ $day ];
            }
            else {
                $temp_values[] = [
                    'facet_value' => $day,
                    'facet_display_value' => ucfirst( $day ),
              		'term_id' => 0,
         			'parent_id' => 0,
          			'depth' => 0,
          			'counter' => 0
                ];
            }
        }

        $args['values'] = $temp_values;
    }

    return $args;
});

// Or, to add different values for 'facet_value' and 'facet_display_value':

add_filter( 'facetwp_facet_render_args', function( $args ) {

  if ( 'business_country' == $args['facet']['name'] ) {

    $lookup = [];
    $temp_values = [];
    $items = [
      'england' => 'England',
      'scotland' => 'Scotland',
      'wales' => 'Wales',
      'northern-ireland' => 'Northern Ireland',
      'ireland' => 'Ireland'
    ];

    foreach ( $args['values'] as $row ) {
      $lookup[ $row['facet_value'] ] = $row;
    }

    foreach ( $items as $key => $value ) {
      if ( isset( $lookup[ $key ] ) ) {
        $temp_values[] = $lookup[ $key ];
      }
      else {
        $temp_values[] = [
          'facet_value' => $key,
          'facet_display_value' => $value,
          'term_id' => 0,
          'parent_id' => 0,
          'depth' => 0,
          'counter' => 0
        ];
      }
    }

    $args['values'] = $temp_values;
  }

  return $args;
});
7 years ago
<?php

add_filter( 'facetwp_facet_render_args', function( $args) {
	if ( 'my_search_facet' == $args['facet']['name'] ) {  // change my_search_facet to name of your facet
		$args['facet']['placeholder'] = 'Something';
	}
	return $args;
} );
8 years ago
//filter color display to maybe add hash to hex colors
add_filter( 'facetwp_facet_render_args', function( $args ) {
	if ( 'colors' == $args['facet']['name'] ) {
	    foreach ( $args['values'] AS $key => $value ) {
	        $args['values'][$key]['facet_display_value'] = ( '' != ( $color = sanitize_hex_color_no_hash( $value['facet_display_value'] ) ) ) ? '#' . $color : $value['facet_display_value'];
        }
	}
	return $args;
});

//filter color index to maybe add hash to hex colors
add_filter( 'facetwp_index_row', function( $params ) {
	if ( 'colors' == $params['facet_name'] ) {
		$display_value = $params['facet_display_value'];
		$params['facet_display_value'] = ( '' != ( $color = sanitize_hex_color_no_hash( $params['facet_display_value'] ) ) ) ? '#' . $color : $params['facet_display_value'];
	}
	return $params;
}, 10 );