From 6626c681757d6a7c4e2af4879fb7b36cd3b9d7c2 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 28 Jul 2015 15:13:25 +0100 Subject: [PATCH] When syncing OLD products, update the attributes to 2.4 standard --- includes/class-wc-product-variable.php | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/includes/class-wc-product-variable.php b/includes/class-wc-product-variable.php index 704f3e6bded..269c24724a0 100644 --- a/includes/class-wc-product-variable.php +++ b/includes/class-wc-product-variable.php @@ -628,6 +628,40 @@ class WC_Product_Variable extends WC_Product { // Loop the variations } else { + /** + * Pre 2.4 handling where 'slugs' were saved instead of the full text attribute. + * Attempt to get full version of the text attribute from the parent and UPDATE meta. + */ + if ( version_compare( get_post_meta( $product_id, '_product_version', true ), '2.4.0', '<' ) ) { + $parent_attributes = array_filter( (array) get_post_meta( $product_id, '_product_attributes', true ) ); + + foreach ( $children as $child_id ) { + $all_meta = get_post_meta( $child_id ); + + foreach ( $all_meta as $name => $value ) { + if ( 0 !== strpos( $name, 'attribute_' ) ) { + continue; + } + if ( sanitize_title( $value[0] ) === $value[0] ) { + foreach ( $parent_attributes as $attribute ) { + if ( $name !== 'attribute_' . sanitize_title( $attribute['name'] ) ) { + continue; + } + $text_attributes = wc_get_text_attributes( $attribute['value'] ); + + foreach ( $text_attributes as $text_attribute ) { + if ( sanitize_title( $text_attribute ) === $value[0] ) { + $value[0] = $text_attribute; + } + } + } + + update_post_meta( $child_id, $name, $value[0] ); + } + } + } + } + // Set the variable product to be virtual/downloadable if all children are virtual/downloadable foreach ( array( '_downloadable', '_virtual' ) as $meta_key ) { $all_variations_yes = true;