base-products-renderer.php

Created Diff never expires
4 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
25 lines
33 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
53 lines
<?php
<?php
namespace ElementorPro\Modules\Woocommerce\Classes;
namespace ElementorPro\Modules\Woocommerce\Classes;


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


abstract class Base_Products_Renderer extends \WC_Shortcode_Products {
abstract class Base_Products_Renderer extends \WC_Shortcode_Products {


/**
/**
* Override original `get_content` that returns an HTML wrapper even if no results found.
* Override original `get_content` that returns an HTML wrapper even if no results found.
*
*
* @return string Products HTML
* @return string Products HTML
*/
*/
public function get_content() {
public function get_content() {
$result = $this->get_query_results();


if ( empty( $result->total ) ) {
/*
return '';
* Before WC 4.0.0, we have to run the same products query twice
* First in get_query_results(), then in get_content() -> product_loop() -> get_query_results()
*/
if ( function_exists( 'WC' ) && version_compare( WC()->version, '4.0.0', '>=' ) ) {
global $wp_query;

// Cache result counts so we don't need to run the same query twice
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' ] );

$total = isset( $this->query_results->total ) ? $this->query_results->total : $wp_query->found_posts;

return empty( $total ) ? '' : $content;
}
}
else {
$result = $this->get_query_results();


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


/**
* Cache the query counts so we don't need to run the same query twice
*/
public function cache_query_results( $results ) {
$this->query_results = $results;
return $results;
}
}
}
}