Skip to content

Commit 0f4eb58

Browse files
committed
Merge branch 'master' of github.com:woocommerce/woocommerce-product-tables-feature-plugin
2 parents b15d9cf + fae55d1 commit 0f4eb58

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

includes/class-wc-product-tables-query.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public function __construct() {
3737
add_filter( 'woocommerce_price_filter_sql', array( $this, 'custom_price_filter_sql' ), 10, 3 );
3838
add_action( 'woocommerce_product_query', array( $this, 'custom_price_filter_args' ) );
3939
}
40+
add_filter( 'posts_results', array( $this, 'prime_product_table_caches' ) );
41+
add_action( 'clean_post_cache', array( $this, 'clean_product_table_caches' ) );
4042
}
4143

4244
/**
@@ -216,4 +218,42 @@ public function custom_price_filter_post_clauses( $args, $wp_query ) {
216218

217219
return $args;
218220
}
221+
222+
/**
223+
* Prime product table caches for a query of multiple products.
224+
*
225+
* @param array $posts Array of post objects.
226+
* @return array
227+
*/
228+
public function prime_product_table_caches( $posts ) {
229+
$prime_ids = array();
230+
231+
foreach ( $posts as $post ) {
232+
if ( ! in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
233+
continue;
234+
}
235+
$prime_ids[] = (int) $post->ID;
236+
}
237+
238+
if ( ! empty( $prime_ids ) ) {
239+
global $wpdb;
240+
241+
$products = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'wc_products WHERE product_id IN (' . implode( ',', $prime_ids ) . ');' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
242+
243+
foreach ( $products as $product ) {
244+
wp_cache_set( 'woocommerce_product_' . $product->product_id, $product, 'product' );
245+
}
246+
}
247+
248+
return $posts;
249+
}
250+
251+
/**
252+
* Clean product table caches for a product.
253+
*
254+
* @param int $product_id Post/product ID.
255+
*/
256+
public function clean_product_table_caches( $product_id ) {
257+
wp_cache_delete( 'woocommerce_product_' . $product_id, 'product' );
258+
}
219259
}

0 commit comments

Comments
 (0)