diff --git a/includes/class-wc-product-variation.php b/includes/class-wc-product-variation.php index cf7e3874c73..0050f7cd258 100644 --- a/includes/class-wc-product-variation.php +++ b/includes/class-wc-product-variation.php @@ -27,7 +27,6 @@ class WC_Product_Variation extends WC_Product_Simple { * @var array */ protected $parent_data = array( - 'name' => '', 'sku' => '', 'manage_stock' => '', 'stock_quantity' => '', @@ -119,11 +118,7 @@ class WC_Product_Variation extends WC_Product_Simple { * @return string */ public function get_name( $context = 'view' ) { - if ( 'view' === $context ) { - return $this->parent_data['name']; - } else { - return $this->get_prop( 'name', $context ); - } + return $this->get_prop( 'name', $context ); } /** @@ -305,6 +300,16 @@ class WC_Product_Variation extends WC_Product_Simple { $this->parent_data = $parent_data; } + /** + * Get the parent data array for this variation. + * + * @since 2.7.0 + * @return array + */ + public function get_parent_data() { + return $this->parent_data; + } + /** * Set attributes. Unlike the parent product which uses terms, variations are assigned * specific attributes using name value pairs. 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 4b233a6e7c5..81726f909e2 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 @@ -55,8 +55,10 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl throw new Exception( sprintf( 'Invalid parent for variation #%d', $product->get_id() ), 422 ); } + $product_name = get_the_title( $post_object ); + $product->set_props( array( - 'name' => get_the_title( $post_object ), + 'name' => $product_name, 'slug' => $post_object->post_name, 'date_created' => $post_object->post_date, 'date_modified' => $post_object->post_modified, @@ -68,6 +70,21 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl $this->read_product_data( $product ); $product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) ); + error_log( print_r( $product_name, 1 ) ); + + if ( __( 'Variation #', 'woocommerce' ) === substr( $product_name, 0, 11 ) ) { + error_log( 'clean up' ); + error_log( print_r( $product_name, 1 ) ); + $parent_data = $product->get_parent_data(); + $new_title = $parent_data['name'] . ' – ' . wc_get_formatted_variation( $product, true, false ); + $product->set_name( $new_title ); + wp_update_post( array( + 'ID' => $product->get_id(), + 'post_title' => $new_title, + ) ); + error_log( print_r( $new_title, 1 ) ); + } + // Set object_read true once all data is read. $product->set_object_read( true ); }