@@ -595,9 +595,7 @@ public function delete( &$product, $args = array() ) {
595595 * @param WC_Product $product The product object.
596596 */
597597 protected function clear_caches ( &$ product ) {
598- foreach ( $ product ->get_attributes ( 'edit ' ) as $ attribute ) {
599- wp_cache_delete ( 'woocommerce_product_attribute_values_ ' . $ attribute ->get_product_attribute_id (), 'product ' );
600- }
598+ wp_cache_delete ( 'woocommerce_product_attribute_values_ ' . $ product ->get_id (), 'product ' );
601599 wp_cache_delete ( 'woocommerce_product_ ' . $ product ->get_id (), 'product ' );
602600 wp_cache_delete ( 'woocommerce_product_relationships_ ' . $ product ->get_id (), 'product ' );
603601 wp_cache_delete ( 'woocommerce_product_downloads_ ' . $ product ->get_id (), 'product ' );
@@ -1348,8 +1346,10 @@ public function read_attributes( &$product ) {
13481346 }
13491347
13501348 if ( ! empty ( $ product_attributes ) ) {
1351- $ attributes = array ();
1352- $ default_attributes = array ();
1349+ $ attributes = array ();
1350+ $ default_attributes = array ();
1351+ $ cached_attr_value_data = wp_cache_get ( 'woocommerce_product_attribute_values_ ' . $ product ->get_id (), 'product ' );
1352+
13531353 foreach ( $ product_attributes as $ attr ) {
13541354 $ attribute = new WC_Product_Attribute ();
13551355 $ attribute ->set_attribute_id ( $ attr ->attribute_id ); // This is the attribute taxonomy ID, or 0 for local attributes.
@@ -1359,20 +1359,17 @@ public function read_attributes( &$product ) {
13591359 $ attribute ->set_visible ( $ attr ->is_visible );
13601360 $ attribute ->set_variation ( $ attr ->is_variation );
13611361
1362- $ attr_value_data = wp_cache_get ( 'woocommerce_product_attribute_values_ ' . $ attr ->product_attribute_id , 'product ' );
1363-
1364- if ( false === $ attr_value_data ) {
1365- $ attr_value_data = $ wpdb ->get_results (
1362+ if ( ! isset ( $ cached_attr_value_data [ $ attr ->product_attribute_id ] ) ) {
1363+ $ cached_attr_value_data [ $ attr ->product_attribute_id ] = $ wpdb ->get_results (
13661364 $ wpdb ->prepare (
13671365 "SELECT value, is_default FROM {$ wpdb ->prefix }wc_product_attribute_values WHERE product_attribute_id = %d " ,
13681366 $ attr ->product_attribute_id
13691367 )
13701368 );
1371-
1372- wp_cache_set ( 'woocommerce_product_attribute_values_ ' . $ attr ->product_attribute_id , $ attr_value_data , 'product ' );
13731369 }
13741370
1375- $ attr_values = wp_list_pluck ( $ attr_value_data , 'value ' );
1371+ $ attr_value_data = $ cached_attr_value_data [ $ attr ->product_attribute_id ];
1372+ $ attr_values = wp_list_pluck ( $ attr_value_data , 'value ' );
13761373
13771374 // If this is a taxonomy, we're reading numeric term IDs.
13781375 if ( $ attr ->attribute_id ) {
@@ -1389,6 +1386,9 @@ public function read_attributes( &$product ) {
13891386 }
13901387 }
13911388 }
1389+
1390+ wp_cache_set ( 'woocommerce_product_attribute_values_ ' . $ product ->get_id (), $ cached_attr_value_data , 'product ' );
1391+
13921392 $ product ->set_attributes ( $ attributes );
13931393 $ product ->set_default_attributes ( $ default_attributes );
13941394 }
0 commit comments