Merge pull request #23132 from woocommerce/fix/23127

Attribute renaming: Fix migration queries
This commit is contained in:
Mike Jolley 2019-04-18 10:58:07 +01:00 committed by GitHub
commit 19a1a7d7db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -576,6 +576,8 @@ function wc_create_attribute( $args ) {
// Update product attributes which use this taxonomy. // Update product attributes which use this taxonomy.
$old_taxonomy_name = 'pa_' . $old_slug; $old_taxonomy_name = 'pa_' . $old_slug;
$new_taxonomy_name = 'pa_' . $data['attribute_name']; $new_taxonomy_name = 'pa_' . $data['attribute_name'];
$old_attribute_key = sanitize_title( $old_taxonomy_name ); // @see WC_Product::set_attributes().
$new_attribute_key = sanitize_title( $new_taxonomy_name ); // @see WC_Product::set_attributes().
$metadatas = $wpdb->get_results( $metadatas = $wpdb->get_results(
$wpdb->prepare( $wpdb->prepare(
"SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_product_attributes' AND meta_value LIKE %s", "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_product_attributes' AND meta_value LIKE %s",
@ -586,13 +588,14 @@ function wc_create_attribute( $args ) {
foreach ( $metadatas as $metadata ) { foreach ( $metadatas as $metadata ) {
$product_id = $metadata['post_id']; $product_id = $metadata['post_id'];
$unserialized_data = maybe_unserialize( $metadata['meta_value'] ); $unserialized_data = maybe_unserialize( $metadata['meta_value'] );
if ( ! $unserialized_data || ! is_array( $unserialized_data ) || ! isset( $unserialized_data[ $old_taxonomy_name ] ) ) {
if ( ! $unserialized_data || ! is_array( $unserialized_data ) || ! isset( $unserialized_data[ $old_attribute_key ] ) ) {
continue; continue;
} }
$unserialized_data[ $new_taxonomy_name ] = $unserialized_data[ $old_taxonomy_name ]; $unserialized_data[ $new_attribute_key ] = $unserialized_data[ $old_attribute_key ];
unset( $unserialized_data[ $old_taxonomy_name ] ); unset( $unserialized_data[ $old_attribute_key ] );
$unserialized_data[ $new_taxonomy_name ]['name'] = $new_taxonomy_name; $unserialized_data[ $new_attribute_key ]['name'] = $new_taxonomy_name;
update_post_meta( $product_id, '_product_attributes', wp_slash( $unserialized_data ) ); update_post_meta( $product_id, '_product_attributes', wp_slash( $unserialized_data ) );
} }