Add 'variations' attribute in products 'extended_info' (https://github.com/woocommerce/woocommerce-admin/pull/1478)

* Add 'variations' attribute in products 'extended_info'

* Add tests
This commit is contained in:
Albert Juhé Lluveras 2019-02-07 09:27:02 +01:00 committed by GitHub
parent 2b0c8089c0
commit 79738b2022
4 changed files with 106 additions and 10 deletions

View File

@ -75,7 +75,8 @@ class ProductsReportTable extends Component {
},
{
label: __( 'Variations', 'wc-admin' ),
key: 'variation',
key: 'variations',
isSortable: true,
},
{
label: __( 'Status', 'wc-admin' ),
@ -95,14 +96,7 @@ class ProductsReportTable extends Component {
const persistedQuery = getPersistedQuery( query );
return map( data, row => {
const {
product_id,
extended_info,
items_sold,
net_revenue,
orders_count,
variations = [],
} = row;
const { product_id, extended_info, items_sold, net_revenue, orders_count } = row;
const {
category_ids,
low_stock_amount,
@ -110,6 +104,7 @@ class ProductsReportTable extends Component {
sku,
stock_status,
stock_quantity,
variations = [],
} = extended_info;
const ordersLink = getNewPath( persistedQuery, 'orders', {
filter: 'advanced',

View File

@ -224,6 +224,12 @@ class WC_Admin_REST_Reports_Products_Controller extends WC_REST_Reports_Controll
'context' => array( 'view', 'edit' ),
'description' => __( 'Product inventory threshold for low stock.', 'wc-admin' ),
),
'variations' => array(
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Product variations IDs.', 'wc-admin' ),
),
'sku' => array(
'type' => 'string',
'readonly' => true,
@ -291,6 +297,7 @@ class WC_Admin_REST_Reports_Products_Controller extends WC_REST_Reports_Controll
'orders_count',
'items_sold',
'product_name',
'variations',
'sku',
),
'validate_callback' => 'rest_validate_request_arg',

View File

@ -40,6 +40,7 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
'stock_quantity' => 'intval',
'low_stock_amount' => 'intval',
'category_ids' => 'array_values',
'variations' => 'array_values',
'sku' => 'strval',
);
@ -69,6 +70,7 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
'stock_quantity',
'low_stock_amount',
'category_ids',
'variations',
'sku',
);
@ -112,6 +114,10 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
$sql_query['from_clause'] .= " JOIN {$wpdb->prefix}postmeta AS postmeta ON {$order_product_lookup_table}.product_id = postmeta.post_id AND postmeta.meta_key = '_sku'";
}
if ( 'variations' === $sql_query['order_by_clause'] ) {
$sql_query['from_clause'] .= " LEFT JOIN ( SELECT post_parent, COUNT(*) AS variations FROM {$wpdb->prefix}posts WHERE post_type = 'product_variation' GROUP BY post_parent ) AS _variations ON {$order_product_lookup_table}.product_id = _variations.post_parent";
}
if ( isset( $query_args['order'] ) ) {
$sql_query['order_by_clause'] .= ' ' . $query_args['order'];
} else {
@ -189,7 +195,14 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
$product = wc_get_product( $product_data['product_id'] );
$extended_attributes = apply_filters( 'woocommerce_rest_reports_products_extended_attributes', $this->extended_attributes, $product_data );
foreach ( $extended_attributes as $extended_attribute ) {
$function = 'get_' . $extended_attribute;
if ( 'variations' === $extended_attribute ) {
if ( ! $product->is_type( 'variable' ) ) {
continue;
}
$function = 'get_children';
} else {
$function = 'get_' . $extended_attribute;
}
if ( is_callable( array( $product, $function ) ) ) {
$value = $product->{$function}();
$extended_info[ $extended_attribute ] = $value;

View File

@ -257,6 +257,87 @@ class WC_Tests_Reports_Products extends WC_Unit_Test_Case {
$this->assertEquals( $expected_data, $data );
}
/**
* Test variable products extended info.
*
* @since 3.5.0
*/
public function test_variable_product_extended_info() {
WC_Helper_Reports::reset_stats_dbs();
// Populate all of the data.
$product = new WC_Product_Variable();
$product->set_name( 'Test Product' );
$product->set_manage_stock( true );
$product->set_stock_quantity( 25 );
$product->set_low_stock_amount( 5 );
$product->save();
$variation = new WC_Product_Variation();
$variation->set_name( 'Test Variation' );
$variation->set_parent_id( $product->get_id() );
$variation->set_attributes( array( 'pa_color' => 'green' ) );
$variation->set_sku( 'test-sku' );
$variation->set_regular_price( 25 );
$variation->set_manage_stock( true );
$variation->set_stock_quantity( 25 );
$variation->set_low_stock_amount( 5 );
$variation->save();
$term = wp_insert_term( 'Test Category', 'product_cat' );
wp_set_object_terms( $product->get_id(), $term['term_id'], 'product_cat' );
$order = WC_Helper_Order::create_order( 1, $variation );
$order->set_status( 'completed' );
$order->set_shipping_total( 10 );
$order->set_discount_total( 20 );
$order->set_discount_tax( 0 );
$order->set_cart_tax( 5 );
$order->set_shipping_tax( 2 );
$order->set_total( 97 ); // $25x4 products + $10 shipping - $20 discount + $7 tax.
$order->save();
WC_Helper_Queue::run_all_pending();
$data_store = new WC_Admin_Reports_Products_Data_Store();
$start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() );
$end_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() + HOUR_IN_SECONDS );
$args = array(
'after' => $start_time,
'before' => $end_time,
'extended_info' => 1,
);
// Test retrieving the stats through the data store.
$data = $data_store->get_data( $args );
// Get updated product data.
$product = wc_get_product( $product->get_id() );
$expected_data = (object) array(
'total' => 1,
'pages' => 1,
'page_no' => 1,
'data' => array(
0 => array(
'product_id' => $product->get_id(),
'items_sold' => 4,
'net_revenue' => 100.0, // $25 * 4.
'orders_count' => 1,
'extended_info' => array(
'name' => $product->get_name(),
'image' => $product->get_image(),
'permalink' => $product->get_permalink(),
'price' => (float) $product->get_price(),
'stock_status' => $product->get_stock_status(),
'stock_quantity' => $product->get_stock_quantity(),
'low_stock_amount' => $product->get_low_stock_amount(),
'category_ids' => array_values( $product->get_category_ids() ),
'sku' => $product->get_sku(),
'variations' => $product->get_children(),
),
),
),
);
$this->assertEquals( $expected_data, $data );
}
/**
* Tests that line item refunds are reflected in product stats.
*/