diff --git a/includes/class-wc-product-variation.php b/includes/class-wc-product-variation.php index 0e6d1dde8f2..ecc436e207d 100644 --- a/includes/class-wc-product-variation.php +++ b/includes/class-wc-product-variation.php @@ -308,10 +308,20 @@ class WC_Product_Variation extends WC_Product_Simple { /** * Set attributes. Unlike the parent product which uses terms, variations are assigned * specific attributes using name value pairs. - * @param array + * @param array $raw_attributes */ - public function set_attributes( $attributes ) { - $this->set_prop( 'attributes', (array) $attributes ); + public function set_attributes( $raw_attributes ) { + $raw_attributes = (array) $raw_attributes; + $attributes = array(); + + foreach ( $raw_attributes as $key => $value ) { + // Add attribute prefix which meta gets stored with. + if ( 0 !== strpos( $key, 'attribute_' ) ) { + $key = 'attribute_' . $key; + } + $attributes[ $key ] = $value; + } + $this->set_prop( 'attributes', $attributes ); } /** diff --git a/includes/data-stores/class-wc-product-variation-data-store-cpt.php b/includes/data-stores/class-wc-product-variation-data-store-cpt.php index ed7ee44454b..7666b8066ab 100644 --- a/includes/data-stores/class-wc-product-variation-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variation-data-store-cpt.php @@ -222,8 +222,8 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl $attributes = $product->get_attributes(); $updated_attribute_keys = array(); foreach ( $attributes as $key => $value ) { - update_post_meta( $product->get_id(), 'attribute_' . $key, $value ); - $updated_attribute_keys[] = 'attribute_' . $key; + update_post_meta( $product->get_id(), $key, $value ); + $updated_attribute_keys[] = $key; } // Remove old taxonomies attributes so data is kept up to date - first get attribute key names.