Gists

by djrmom - 7 hours ago

<?php
/** use query in advanced mode https://d.pr/i/FOJSwM for facetwp in default WP search page **/
return [
  "post_type" => "any",
  "post_status" => [
    "publish"
  ],
  "posts_per_page" => get_option( 'posts_per_page' ),
  "s" => get_query_var( 's' )
];
by mgibbs189 - 2 days ago

<?php

// Add to your (child) theme's functions.php

add_filter( 'facetwp_template_html', function( $output, $class ) {
    $output = ImagifyWebpPictureDisplay::get_instance()->process_content( $output );
    return $output;
}, 10, 2 );
by djrmom - 1 week ago

<?php
/** schedule one time index **/
add_action( 'pmxi_after_xml_import', function() {
	wp_schedule_single_event( time(), 'fwp_single_index', array() );
});

/** needs to be availble to cron to run **/
function fwp_single_index() {
    FWP()->indexer->index();
}
add_action( 'fwp_single_index', 'fwp_single_index' );
by mgibbs189 - 1 week ago

<?php
namespace ElementorProModulesWoocommerceClasses;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

abstract class Base_Products_Renderer extends WC_Shortcode_Products {

	public $query_results;

	/**
	 * Override original `get_content` that returns an HTML wrapper even if no results found.
	 *
	 * @return string Products HTML
	 */
	public function get_content() {


		/**
		 * Output an empty string if there are no results
		 * 
		 * The `get_content()` method triggers `get_query_results`, so it's
		 * actually being called twice in WooCommerce < 4.0.0
		 * 
		 * The `woocommerce_shortcode_products_query_results` hook was added
		 * in WooCommerce 4.0.0. It lets us cache the query counts
		 * so that only 1 products query needs to be executed.
		 */
		if ( function_exists( 'WC' ) && version_compare( WC()->version, '4.0.0', '>=' ) ) {

			add_filter( 'woocommerce_shortcode_products_query_results', [ $this, 'cache_query_results'] );
			$content = parent::get_content();
			remove_filter( 'woocommerce_shortcode_products_query_results', [ $this, 'cache_query_results'] );

			if ( isset( $this->query_results->total ) ) {
				$total = $this->query_results->total;
			}
			else {
				$total = $GLOBALS['wp_query']->found_posts;
			}

			return empty( $total ) ? '' : $content;
		}

		// WooCommerce < 4.0.0
		$result = $this->get_query_results();

		if ( empty( $result->total ) ) {
			return '';
		}

		return parent::get_content();
	}


	/**
	 * Cache the query results so we don't need to run
	 * a separate query just to grab the total count
	 * 
	 * @return object Results data
	 */
	public function cache_query_results( $results ) {
		$this->query_results = $results;
		return $results;
	}
}

by mgibbs189 - 2 weeks ago

<?php

// Add the following to your (child) theme's functions.php

add_action( 'wp_footer', function() {
?>

<script>
(function($) {
  $(document).on('click', 'a[href^="#"]', function() {
    var css_id = $(this).attr('href');
    var $el = $(css_id);
    if (0 < $el.length) {
      window.scrollTo(0, $el.offset().top);
    }
  });
})(jQuery);
</script>

<?php
}, 100 );

by mgibbs189 - 3 weeks ago

<?php

// Add to your (child) theme's functions.php

add_filter( 'pre_get_posts', function( $query ) {
    if ( $query->is_post_type_archive( 'reviews' ) ) {
        $query->set( 'facetwp', false );
    }
});
by mgibbs189 - 4 weeks ago

<?php
namespace ElementorProModulesWoocommerceClasses;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

abstract class Base_Products_Renderer extends WC_Shortcode_Products {

	public $query_results;

	/**
	 * Override original `get_content` that returns an HTML wrapper even if no results found.
	 *
	 * @return string Products HTML
	 */
	public function get_content() {

		// WooCommerce 4.0.0 and above
		if ( function_exists( 'WC' ) && version_compare( WC()->version, '4.0.0', '>=' ) ) {
			add_filter( 'woocommerce_shortcode_products_query_results', [ $this, 'store_query_results'] );
			$content = parent::get_content();
			remove_filter( 'woocommerce_shortcode_products_query_results', [ $this, 'store_query_results'] );

			return $this->query_results->total ? $content : '';
		}

		// WooCommerce < 4.0.0
		$result = $this->get_query_results();

		if ( empty( $result->total ) ) {
			return '';
		}

		return parent::get_content();
	}


	/**
	 * Prevent from running get_query_results() twice
	 * 
	 * @return object Results data
	 */
	public function store_query_results( $results ) {
		$this->query_results = $results;
		return $results;
	}
}

by djrmom - 4 weeks ago

<?php
namespace ElementorProModulesWoocommerceClasses;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

abstract class Base_Products_Renderer extends WC_Shortcode_Products {
	public $query_results;

	/**
	 * Override original `get_content` that returns an HTML wrapper even if no results found.
	 *
	 * @return string Products HTML
	 */
	public function get_content() {
		
		add_filter( 'woocommerce_shortcode_products_query_results', [ $this, 'grab_query_results' ] );
		$content = parent::get_content();
		remove_filter( 'woocommerce_shortcode_products_query_results', [ $this, 'grab_query_results' ] );

		return $this->query_results->total ? $content : '';
	}

	/**
	 * Grab the shortcode pager data to prevent running $this->get_query_results() twice
	 * 
	 * @return object Results data
	 */
	public function grab_query_results( $results ) {
		$this->query_results = $results;
		return $results;
	}
}
by djrmom - 1 month ago

<?php
/** return custom data in the json facet returns on refresh
 ** access with FWP.settings.my_data in JS
 */
add_filter( 'facetwp_render_output', function( $output, $params ) {
    $output['settings']['my_data'] = 'some data';
    return $output;
}, 10, 2 );
by djrmom - 2 weeks ago

<?php
/** sample of css and js to create a custom loading animation to cover the full facet area
 ** change #facet-container to a id or class of container element for your facets
 ** update css as needed
 ** please see https://facetwp.com/documentation/support/ for questions
 **/

add_action( 'wp_head', function() { ?>
    <style>
		#facet-container {
			position: relative;
		}
		.custom-loader {
			position: absolute;
			background-image: url("/path/to/image");
			background-position: center;
			background-repeat: no-repeat;
		}
    </style>
<?php });

add_action( 'wp_head', function() {
	?>
	<script>
	(function($) {
		$(document).on('facetwp-refresh', function() {
			if (FWP.loaded) {
				$('#facet-container').prepend('<div class="custom-loader facetwp-loading"></div>');
				$('.custom-loader').height( $('#facet-container').height() ).width( $('#facet-container').width() );
			}
		});
	
		$(document).on('facetwp-loaded', function() {
			$('.custom-loader').remove();
		});
	})(jQuery);  
	</script>
	<?php
},100);