Caches call to load product variation attributes
This commit adds product variation attributes to WP cache when they are first loaded to avoid running the same database queries when the same product is loaded multiple times. This cache will be invalidated whenever product attributes are changed. Fixes #17120
This commit is contained in:
parent
fb70ae3fea
commit
a3f14c7256
|
@ -98,6 +98,13 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple
|
|||
$variation_attributes = array();
|
||||
$attributes = $product->get_attributes();
|
||||
$child_ids = $product->get_children();
|
||||
$cache_key = WC_Cache_Helper::get_cache_prefix( 'products' ) . 'product_variation_attributes_' . $product->get_id();
|
||||
$cache_group = 'products';
|
||||
$cached_data = wp_cache_get( $cache_key, $cache_group );
|
||||
|
||||
if ( false !== $cached_data ) {
|
||||
return $cached_data;
|
||||
}
|
||||
|
||||
if ( ! empty( $child_ids ) && ! empty( $attributes ) ) {
|
||||
foreach ( $attributes as $attribute ) {
|
||||
|
@ -140,6 +147,8 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple
|
|||
}
|
||||
}
|
||||
|
||||
wp_cache_set( $cache_key, $variation_attributes, $cache_group );
|
||||
|
||||
return $variation_attributes;
|
||||
}
|
||||
|
||||
|
|
|
@ -126,9 +126,18 @@ function wc_delete_product_transients( $post_id = 0 ) {
|
|||
// Does this product have a parent?
|
||||
$product = wc_get_product( $post_id );
|
||||
|
||||
if ( $product && $product->get_parent_id() > 0 ) {
|
||||
if ( $product ) {
|
||||
if ( $product->get_parent_id() > 0 ) {
|
||||
wc_delete_product_transients( $product->get_parent_id() );
|
||||
}
|
||||
|
||||
if ( 'variable' === $product->get_type() ) {
|
||||
wp_cache_delete(
|
||||
WC_Cache_Helper::get_cache_prefix( 'products' ) . 'product_variation_attributes_' . $product->get_id(),
|
||||
'products'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete transients.
|
||||
|
|
Loading…
Reference in New Issue