Base variation report endpoint extended info off of the parent product if variation ID is 0.

This commit is contained in:
Jeff Stieler 2019-08-17 10:23:24 -07:00
parent 60ff4fc067
commit 150feee517
1 changed files with 31 additions and 24 deletions

View File

@ -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 ) );