Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Created March 31, 2020 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgibbs189/241a616c53186e6231766c0e21cad587 to your computer and use it in GitHub Desktop.
Save mgibbs189/241a616c53186e6231766c0e21cad587 to your computer and use it in GitHub Desktop.
Fix for Elementor Pro "base-products-renderer.php"
<?php
namespace ElementorPro\Modules\Woocommerce\Classes;
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment