diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 725fe1d47eb..0f8452fb16a 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -119,6 +119,10 @@ class WC_Install { ), '3.5.2' => array( 'wc_update_352_drop_download_log_fk', + ), + '3.5.3' => array( + 'wc_update_353_db_version', + 'wc_update_353_set_attrib_values_to_excerpt', ) ); diff --git a/includes/wc-update-functions.php b/includes/wc-update-functions.php index 37cd676168a..683a2baa113 100644 --- a/includes/wc-update-functions.php +++ b/includes/wc-update-functions.php @@ -1894,3 +1894,32 @@ function wc_update_352_drop_download_log_fk() { $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_download_log DROP FOREIGN KEY fk_wc_download_log_permission_id" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared } } + +/** + * Update DB Version. + */ +function wc_update_353_db_version() { + WC_Install::update_db_version( '3.5.3' ); +} + +/* + * Add attribute values to post_excerpt to allow searching variations without JOINs for each attribute. + */ +function wc_update_353_set_attrib_values_to_excerpt() { + global $wpdb; + + $variation_ids = $wpdb->get_results( "SELECT ID from {$wpdb->prefix}posts WHERE post_type='product_variation'", ARRAY_A ); + foreach ( $variation_ids as $variation_id ) { + $variation_id = $variation_id['ID']; + $variation_product = wc_get_product( $variation_id ); + $attribute_values = wc_get_formatted_variation( $variation_product, true, false ); + if ( '' !== $attribute_values ) { + $wpdb->query( + $wpdb->prepare( + "UPDATE {$wpdb->prefix}posts SET post_excerpt=%s WHERE ID=%d", + $attribute_values, $variation_id + ) + ); + } + } +}