Gists

8 hours ago
<?php
// If you are using the Submit Button add-on and want the redirect not to happen when your facet is empty, 
// use the following script and de-activate the add-on. This is basically the same as facetwp-submit.js, but with an 
// extra check in L13-14 to check if a specific facet is empty when the Submit button is clicked. If it is, the redirect does not happen.
// This can be useful to prevent 'empty' redirects for facet types that refresh themselves, like an Autocomplete facet that submits on Enter or clicking a selection.

add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    (function($) {
      $().on('click', '.fwp-submit', function() {
        FWP.parseFacets();
        
        // Do nothing if this facet is empty
        if ( ! FWP.facets['my_facet_name'].length ) { // Replace 'my_facet_name' with the name of your facet
          return;
        }

        var href = $(this).attr('data-href');
        var query_string = FWP.buildQueryString();

        if (query_string.length) {
          var prefix = (-1 < href.indexOf('?')) ? '&' : '?';
          href += prefix + query_string;
        }

        window.location.href = href;
      });
    })(fUtil);
  </script>
  <?php
}, 100 );
14 hours ago
<?php
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {

  if ( 'el-isuok7' == $item['settings']['name'] ) { // Replace 'el-isuok7' with the'Unique name' of your Listing Builder repeater item

    $fieldvalues = get_field( 'automerkit' ); // Replace 'automerkit' with the name of your Repeater field
    if( $fieldvalues ) {
      $output = array_map(function($arr) {
        return implode(', ', $arr);
      }, array_map('array_values', $fieldvalues));
      $value = implode(', ', $output);
   }

  }
  return $value;

}, 10, 2 );
4 days ago
<?php
// Use readonly instead of disabled to for "Copy" Metabox fields in WPML 
// See: https://facetwp.com/help-center/using-facetwp-with/meta-box/#fix-indexing-issues-when-using-meta-box-with-wpml

add_filter( 'rwmb_text_html', function( $html ) {
    return str_replace( 'disabled', 'readonly', $html );
}, 10 );
2 weeks ago
<!--To be used in a Listing Builder listing in Dev mode-->
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ): the_post(); ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php endwhile; ?>

<!--Adds an extra post item (for example an ad) at the end of the query-->
<?php if ( $wp_query->max_num_pages == FWP()->facet->ajax_params['paged'] ) : ?>
<div>This is my extra post item<div>
<?php endif; ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
3 weeks ago
<style>
/**
 ** replace test_facet with name of your fselect
 **/
    .facetwp-facet-test_facet .fs-dropdown {
        position: relative;
        border: none;
    }

    .facetwp-facet-test_facet .fs-dropdown.fs-hidden {
        display: block;
        position: relative;
    }

    .facetwp-facet-test_facet .fs-dropdown .fs-search {
        display: none;
    }

    .facetwp-facet-test_facet .fs-label-wrap {
        display: none;
    }

    .facetwp-facet-test_facet .fs-dropdown .fs-options {
        overflow: visible;
        max-height: none
    }
</style>
3 weeks ago
<?php
// Syncs the selected values of two facets with different types when using one of the facets.
// The facets need to use the same Data Source.
// Change 'categories_radio' and 'categories_dropdown' to the names of your facets.
// Caveat: both facets will ghost each other's choices. This may not be optimal/desired.
// This can be fixed with the second snippet. Until a Dropdown facet has ghosts, this is not (yet) possible for Dropdowns.
add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {
      if (null !== FWP.active_facet) {
        if ( 'categories_radio' == fUtil(FWP.active_facet.nodes[0]).attr('data-name' ) ) {
          FWP.facets['categories_dropdown'] = FWP.facets['categories_radio'];
        } else if ( 'categories_dropdown' == fUtil(FWP.active_facet.nodes[0]).attr('data-name' ) ) {
          FWP.facets['categories_radio'] = FWP.facets['categories_dropdown'];
        }
      }
    });
  </script>
  <?php
}, 100 );

// Optional: remove unclickable ghosts from the Radio facet
add_filter( 'facetwp_facet_html', function( $output, $params ) {
    if ( 'categories_radio' == $params['facet']['name'] ) { 
        $output = str_replace ( 'disabled' , '' , $output );
    }
    return $output;
}, 10, 2 );
4 weeks ago
<?php
/** get_price for woocommerce grouped products **/
add_filter( 'facetwp_indexer_post_facet', function( $bypass, $params ) {

    $defaults = $params['defaults'];
    $post_id = (int) $defaults['post_id'];
    $post_type = get_post_type( $post_id );

    if ( $bypass || 'product' != $post_type ) {
        // skip if $bypass is already true
        // skip if not product post type
        return $bypass; 
    }    

    $product = wc_get_product( $post_id );

    if ( ! $product || 'grouped' != $product->get_type() ) {
        // skip if not product not found
        // skip if not grouped product
        return $bypass; // skip
    }


    if ( 'price' == $params['facet']['name'] ) { // change 'price' for name of your facet, this is for use with Woocommerce -> Price selected as the data source in the facet settings

		$child_prices     = array();
		$children         = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' );

		foreach ( $children as $child ) {
			if ( '' !== $child->get_price() ) {
				$child_prices[] = $child->get_price();
			}
		}

		if ( ! empty( $child_prices ) ) {
			$min_price = min( $child_prices );
			$max_price = max( $child_prices );
		} else {
			$min_price = '';
			$max_price = '';
		}

        if ( '' != $min_price ) {

            $defaults['facet_value'] = $min_price;
            $defaults['facet_display_value'] = $max_price;
            FWP()->indexer->index_row( $defaults );

            return true;

        }
    }

    return $bypass;

}, 10, 3 );
1 month ago
<?php
// Remove empty facet choices, if you can't find the root cause.
// Re-index after adding this snippet.
add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( '' == $params['facet_display_value'] ) {
    $params['facet_value'] = ''; // Skip indexing
  }
  return $params;
}, 10, 2 );
3 weeks ago
<?php
// Note: in FacetWP 4.3.2 the facetwp_i18n hook was added to the Button item, making it easier to translate the Button text.
// See: https://facetwp.com/help-center/listing-templates/listing-builder/#translate-the-button-item-text
// The solutions below can still be used for older FacetWP versions and other items.


// To tranlate a Button item's Button text:

add_filter( 'facetwp_builder_item_value', function( $value, $item ) {

  if ( 'el-2epdx' == $item['settings']['name']  ) { // Replace 'el-2epdx' with the name of your listing builder item

    $lang = ( ! empty( FWP()->facet->http_params['lang'] ) ) ? FWP()->facet->http_params['lang'] : apply_filters( 'wpml_current_language', null );
    switch ( $lang ) {
      case 'en' :
        $value = 'My english button text';
        break;
      default :
        $value = 'Default button text';
    }
  }

  return $value;
}, 10, 2 );


// If the Button has a link set, use this instead:
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {

  if ( 'el-pgnldu' == $item['settings']['name']  ) { // Replace 'el-2epdx' with the name of your listing builder item

    $lang = ( ! empty( FWP()->facet->http_params['lang'] ) ) ? FWP()->facet->http_params['lang'] : apply_filters( 'wpml_current_language', null );

    switch ( $lang ) {
      case 'en' :
        $text = 'My english button text';
        break;
      default :
        $text = 'Default button text';
    }

    $dom = new DOMDocument();
    $dom->loadHTML($value);

    $link = $dom->getElementsByTagName('a')->item(0);
    $link->nodeValue = $text;

    $value= $dom->saveHTML();
  }

  return $value;
}, 10, 2 );
2 months ago
<?php
// If you are using an ACF Color Picker field as facet data source,
// you can use the color value (hex or rgb(a)) to give a (background) color to the facet choice.
// It is important to use the facet_display_value for the color, and not the facet_value, 
// because the facet_value will be hashed into a random string of numbers due to unsafe URL characters.
// Note: for fSelect facets, you need to use esc_html() around the label output in L11, otherwise the HTML will be stripped out.
// More info: https://facetwp.com/help-center/using-facetwp-with/advanced-custom-fields/#using-a-color-picker-field

add_filter( 'facetwp_facet_display_value', function( $label, $params ) {
  if ( 'my_color_facet' == $params['facet']['name'] ) { // Replace 'my_color_facet' with the name of your color picker based facet.
    $val = $params['row']['facet_display_value']; // Use the display value here to get the unhashed hex or rgb(a) color value.
    $label = '<span style="background-color:' . $val . ';">' . $label . '</span>'; // Add a span to the facet choice with the color as CSS background color.
  }
  return $label;
}, 20, 2 );