Gists

7 days ago
<?php

/** give your query an id of "use_facet" **/

add_action( 'elementor/query/use_facet', function( $query ) {
	$query->set( 'facetwp', true );
} );

add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
	if ( true !== $query->get( 'facetwp', false ) ) {
		return false;
	}
	return $is_main_query;

}, 10, 2 );
1 week ago
<?php

/**
 * 1. Add to your (child) theme's functions.php
 * 2. Assumes a facet named "doctor_location" w/ an ACF relationship field data source
 * 3. Assumes that the ACF Map field name is "location"
 */
add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'doctor_location' == $params['facet_name'] ) {
        $location_id = (int) $params['facet_value'];
        $location = get_field( 'location', $location_id );
        $params['facet_value'] = empty( $location ) ? '' : $location['lat'];
        $params['facet_display_value'] = empty( $location ) ? '' : $location['lng'];
    }
    return $params;
}, 10, 2 );
2 weeks ago
<?php

/** for an on sale product query add this to query args, use in advanced query view
 ** for layout templates - ex. https://d.pr/i/p5hxWm
 ** "post__in" => array_merge( [0], wc_get_product_ids_on_sale() )
 **/

/** this may cause facet to detect the query used in wc_get_product_ids_on_sale
 ** use this filter to prevent
 **/
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
    if ( 0 > $query->get( 'posts_per_page', 0 ) ) { // skips in query for -1 as post_per_page
        $is_main_query = false;
    }
    return $is_main_query;
}, 10, 2 );
2 weeks ago
add_action( 'wp_head', function() {
    ?>
    <script>
    document.addEventListener('DOMContentLoaded', function () {
        if ( 'undefined' !== typeof FWP && 'undefined' !== typeof FWP.hooks) {
            FWP.hooks.addFilter('facetwp/set_options/fselect', function(opts, facet) {
                if (facet.facet_name == 'categories') {
                    opts.optionFormatter = function(label, node) {
                        var counter = node.getAttribute('data-counter');
                        return (counter) ? label + ' <span class="my-class">' + counter + '</span>' : label;
                    };
                    return opts;
                }
            });
        }
    });
    </script>
    <?php
}, 100);
3 weeks ago
<?php

// Add to your (child) theme's functions.php
// replace "YOUR_FACET_NAME" with your *actual* facet name

add_filter( 'facetwp_render_output', function( $output ) {
    $output['settings']['YOUR_FACET_NAME']['range']['min']['minDate'] = date( 'Y-m-d' );
    return $output;
});
3 weeks ago
<?php

/**
 *
 * This assumes a facet named "product_catalogue" using "product_cats" taxonomy
 *
 * If the facet is in use, it will send to the browser an object containing terms
 * (type `FWP_JSON.product_cat` into the browser console)
 *
 */
add_filter( 'facetwp_assets', function( $assets ) {
    $facet_name = 'product_catalogue';
    $taxonomy = 'product_cat';

    if ( isset( FWP()->facet->facets[ $facet_name ] ) ) {
        $selected = FWP()->facet->facets[ $facet_name ]['selected_values'];

        if ( ! empty( $selected ) ) {
            FWP()->display->json[ $taxonomy ] = FWP()->helper->get_term_depths( $taxonomy );
        }
    }

    return $assets;
});
4 weeks ago
<?php
/** convert time sources for wp recipe manager to hour / min values **/
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
    if ( preg_match( '/wprm\/([a-zA-Z]+)_time/', $item['source'], $matches ) ) {

		if ( !empty( $matches ) ) {
			$type = $matches[1];
		} else {
			return $value;
		}		
        
		$recipe_id = get_the_ID();

		if ( 'post' == get_post_type( get_the_ID() ) ) {
			$recipe_ids = WPRM_Recipe_Manager::get_recipe_ids_from_post( get_the_ID() );
			if ( !empty( $recipe_ids ) ) {
				$recipe_id = array_shift( $recipe_ids );
			}
			
		}

		if ( 0 < $recipe_id) {
			return do_shortcode( '[wprm-recipe-time type="' . $type . '" id=" ' . $recipe_id . '"]' );
		}
		
    }
    return $value;
}, 10, 2 );
2 months ago
<?php
/*
* Manually update the upt_user post when a specific usermeta field is updated.
* @keywords EDD Front End Submission
*/

add_action( 'updated_user_meta', function( $meta_id, $object_id, $meta_key, $_meta_value ) {

    // Only run for "profile_active" field
    if( $meta_key == 'profile_active' ) {

    	// Get post ID and manually update meta
    	$postid = get_user_meta( $object_id, '_upt_post_id', TRUE );
    	update_post_meta( $postid, 'meta-profile_active', $_meta_value );

    	// Run FacetWP Indexer
    	FWP()->indexer->index( $postid );
    }
}, 10, 4 );
2 months ago
<?php

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'stock' == $params['facet_name'] && 1 == $params['facet_value'] ) {
        $post_id = (int) $params['post_id'];
        $product = wc_get_product( $post_id );
        if ( 'instock' !== $product->get_stock_status() ) {
            $params['facet_value'] = 0;
            $params['facet_display_value'] = 'Out of Stock';
        }
    }
    return $params;
}, 10, 2 );
2 months ago
<?php

add_filter( 'gettext', function( $translated_text, $text, $domain ) {
	if ( 'fwp-front' == $domain ) {
        if ( 'Enter {n} or more characters' == $translated_text ) {
			$translated_text = 'Geben Sie {n} oder mehr Zeichen ein';
        }
        else if ( 'Loading' == $translated_text ) {
            $translated_text = 'Wird geladen';
        }
        else if ( 'No results' == $translated_text ) {
            $translated_text = 'Keine Ergebnisse';
        }
	}
	return $translated_text;
}, 10, 3 );