From 150feee51789977acaadc90f155df66cbdb1f5bc Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Sat, 17 Aug 2019 10:23:24 -0700 Subject: [PATCH] Base variation report endpoint extended info off of the parent product if variation ID is 0. --- .../src/API/Reports/Variations/DataStore.php | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/plugins/woocommerce-admin/src/API/Reports/Variations/DataStore.php b/plugins/woocommerce-admin/src/API/Reports/Variations/DataStore.php index b955cf94642..1291783f074 100644 --- a/plugins/woocommerce-admin/src/API/Reports/Variations/DataStore.php +++ b/plugins/woocommerce-admin/src/API/Reports/Variations/DataStore.php @@ -177,32 +177,39 @@ class DataStore extends ReportsDataStore implements DataStoreInterface { $extended_attributes = apply_filters( 'woocommerce_rest_reports_variations_extended_attributes', $this->extended_attributes, $product_data ); $product = wc_get_product( $product_data['product_id'] ); $variations = array(); - if ( method_exists( $product, 'get_available_variations' ) ) { - $variations = $product->get_available_variations(); - } - foreach ( $variations as $variation ) { - if ( (int) $variation['variation_id'] === (int) $product_data['variation_id'] ) { - $attributes = array(); - $variation_product = wc_get_product( $variation['variation_id'] ); - foreach ( $extended_attributes as $extended_attribute ) { - $function = 'get_' . $extended_attribute; - if ( is_callable( array( $variation_product, $function ) ) ) { - $value = $variation_product->{$function}(); - $extended_info[ $extended_attribute ] = $value; - } - } - foreach ( $variation['attributes'] as $attribute_name => $attribute ) { - $name = str_replace( 'attribute_', '', $attribute_name ); - $option_term = get_term_by( 'slug', $attribute, $name ); - $attributes[] = array( - 'id' => wc_attribute_taxonomy_id_by_name( $name ), - 'name' => str_replace( 'pa_', '', $name ), - 'option' => $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $attribute, - ); - } - $extended_info['attributes'] = $attributes; + + // Base extended info off the parent variable product if the variation ID is 0. + // This is caused by simple products with prior sales being converted into variable products. + // See: https://github.com/woocommerce/woocommerce-admin/issues/2719. + $variation_id = (int) $product_data['variation_id']; + $variation_product = ( 0 === $variation_id ) ? $product : wc_get_product( $variation_id ); + $attributes = array(); + + foreach ( $extended_attributes as $extended_attribute ) { + $function = 'get_' . $extended_attribute; + if ( is_callable( array( $variation_product, $function ) ) ) { + $value = $variation_product->{$function}(); + $extended_info[ $extended_attribute ] = $value; } } + + // If this is a variation, add its attributes. + if ( 0 < $variation_id ) { + $variation_attributes = $variation_product->get_variation_attributes(); + + foreach ( $variation_attributes as $attribute_name => $attribute ) { + $name = str_replace( 'attribute_', '', $attribute_name ); + $option_term = get_term_by( 'slug', $attribute, $name ); + $attributes[] = array( + 'id' => wc_attribute_taxonomy_id_by_name( $name ), + 'name' => str_replace( 'pa_', '', $name ), + 'option' => $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $attribute, + ); + } + } + + $extended_info['attributes'] = $attributes; + // If there is no set low_stock_amount, use the one in user settings. if ( '' === $extended_info['low_stock_amount'] ) { $extended_info['low_stock_amount'] = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );