From 90f227da697b54f2cc2ed57d76b2fdb6f258849c Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 6 Sep 2017 14:36:14 +0100 Subject: [PATCH 1/2] Shortcode rand sorting --- includes/shortcodes/class-wc-shortcode-products.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/includes/shortcodes/class-wc-shortcode-products.php b/includes/shortcodes/class-wc-shortcode-products.php index 5df396ec336..bc9e74f6ee8 100644 --- a/includes/shortcodes/class-wc-shortcode-products.php +++ b/includes/shortcodes/class-wc-shortcode-products.php @@ -436,8 +436,16 @@ class WC_Shortcode_Products { * @return WP_Query */ protected function get_products() { - $transient_name = 'wc_loop' . substr( md5( wp_json_encode( $this->query_args ) . $this->type ), 28 ) . WC_Cache_Helper::get_transient_version( 'product_query' ); - $products = get_transient( $transient_name ); + $transient_name = 'wc_loop' . substr( md5( wp_json_encode( $this->query_args ) . $this->type ), 28 ); + + if ( 'rand' === $this->query_args['orderby'] ) { + // When using rand, we'll cache a number of random queries and pull those to avoid querying rand on each page load. + $rand_index = rand( 0, max( 1, absint( apply_filters( 'woocommerce_product_query_max_rand_cache_count', 5 ) ) ) ); + $transient_name .= $rand_index; + } + + $transient_name .= WC_Cache_Helper::get_transient_version( 'product_query' ); + $products = get_transient( $transient_name ); if ( false === $products || ! is_a( $products, 'WP_Query' ) ) { if ( 'top_rated_products' === $this->type ) { From 01730cdc4ff429c11de880deb8c05ec4008ba490 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 31 Oct 2017 12:58:36 +0000 Subject: [PATCH 2/2] Add method to generate transient name --- .../class-wc-shortcode-products.php | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/includes/shortcodes/class-wc-shortcode-products.php b/includes/shortcodes/class-wc-shortcode-products.php index bc9e74f6ee8..5fb33ffb4fe 100644 --- a/includes/shortcodes/class-wc-shortcode-products.php +++ b/includes/shortcodes/class-wc-shortcode-products.php @@ -430,12 +430,12 @@ class WC_Shortcode_Products { } /** - * Get products. + * Generate and return the transient name for this shortcode based on the query args. * - * @since 3.2.0 - * @return WP_Query + * @since 3.3.0 + * @return string */ - protected function get_products() { + protected function get_transient_name() { $transient_name = 'wc_loop' . substr( md5( wp_json_encode( $this->query_args ) . $this->type ), 28 ); if ( 'rand' === $this->query_args['orderby'] ) { @@ -445,7 +445,19 @@ class WC_Shortcode_Products { } $transient_name .= WC_Cache_Helper::get_transient_version( 'product_query' ); - $products = get_transient( $transient_name ); + + return $transient_name; + } + + /** + * Get products. + * + * @since 3.2.0 + * @return WP_Query + */ + protected function get_products() { + $transient_name = $this->get_transient_name(); + $products = get_transient( $transient_name ); if ( false === $products || ! is_a( $products, 'WP_Query' ) ) { if ( 'top_rated_products' === $this->type ) {