Show full variation name instead of parent product in orders report. (https://github.com/woocommerce/woocommerce-admin/pull/5056)

This commit is contained in:
Jeff Stieler 2020-09-01 08:22:36 -04:00 committed by GitHub
parent fb03e64ced
commit bc39bdf646
2 changed files with 137 additions and 8 deletions

View File

@ -295,7 +295,6 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
protected function include_extended_info( &$orders_data, $query_args ) {
$mapped_orders = $this->map_array_by_key( $orders_data, 'order_id' );
$products = $this->get_products_by_order_ids( array_keys( $mapped_orders ) );
$mapped_products = $this->map_array_by_key( $products, 'product_id' );
$coupons = $this->get_coupons_by_order_ids( array_keys( $mapped_orders ) );
$customers = $this->get_customers_by_orders( $orders_data );
$mapped_customers = $this->map_array_by_key( $customers, 'customer_id' );
@ -307,7 +306,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
}
$mapped_data[ $product['order_id'] ]['products'][] = array(
'id' => $product['product_id'],
'id' => '0' === $product['variation_id'] ? $product['product_id'] : $product['variation_id'],
'name' => $product['product_name'],
'quantity' => $product['product_quantity'],
);
@ -364,12 +363,24 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$included_order_ids = implode( ',', $order_ids );
$products = $wpdb->get_results(
"SELECT order_id, ID as product_id, post_title as product_name, product_qty as product_quantity
FROM {$wpdb->posts}
JOIN {$order_product_lookup_table} ON {$order_product_lookup_table}.product_id = {$wpdb->posts}.ID
WHERE
order_id IN ({$included_order_ids})
",
"SELECT
order_id,
product_id,
variation_id,
post_title as product_name,
product_qty as product_quantity
FROM {$wpdb->posts}
JOIN
{$order_product_lookup_table}
ON {$wpdb->posts}.ID = (
CASE WHEN variation_id > 0
THEN variation_id
ELSE product_id
END
)
WHERE
order_id IN ({$included_order_ids})
",
ARRAY_A
); // WPCS: cache ok, DB call ok, unprepared SQL ok.

View File

@ -0,0 +1,118 @@
<?php
/**
* Orders Report tests.
*
* @package WooCommerce\Admin\Tests\Orders
*/
use \Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore as OrdersDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Orders\Query as OrdersQuery;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/**
* Class WC_Tests_Reports_Orders
*/
class WC_Tests_Reports_Orders extends WC_Unit_Test_Case {
/**
* Test that extended info handles variations correctly.
*/
public function test_extended_info() {
global $wpdb;
WC_Helper_Reports::reset_stats_dbs();
// Populate all of the data.
$parent_product = new WC_Product_Variable();
$parent_product->set_name( 'Variable Product' );
$parent_product->set_regular_price( 25 );
$attribute = new WC_Product_Attribute();
$attribute->set_id( 0 );
$attribute->set_name( 'pa_color' );
$attribute->set_options( explode( WC_DELIMITER, 'green | red' ) );
$attribute->set_visible( false );
$attribute->set_variation( true );
$parent_product->set_attributes( array( $attribute ) );
$parent_product->save();
$variation = new WC_Product_Variation();
$variation->set_name( 'Test Variation' );
$variation->set_parent_id( $parent_product->get_id() );
$variation->set_regular_price( 10 );
$variation->set_attributes( array( 'pa_color' => 'green' ) );
$variation->set_manage_stock( true );
$variation->set_stock_quantity( 25 );
$variation->save();
$simple_product = new WC_Product_Simple();
$simple_product->set_name( 'Simple Product' );
$simple_product->set_regular_price( 25 );
$simple_product->save();
$order = WC_Helper_Order::create_order( 1, $variation );
// Add simple product.
$item = new WC_Order_Item_Product();
$item->set_props(
array(
'product' => $simple_product,
'quantity' => 1,
'subtotal' => wc_get_price_excluding_tax( $simple_product, array( 'qty' => 1 ) ),
'total' => wc_get_price_excluding_tax( $simple_product, array( 'qty' => 1 ) ),
)
);
$item->save();
$order->add_item( $item );
// Fix totals.
$order->set_total( 75 ); // ( 4 * 10 ) + 25 + 10 shipping (in helper).
$order->set_status( 'completed' );
$order->save();
WC_Helper_Queue::run_all_pending();
$data_store = new OrdersDataStore();
$start_time = gmdate( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() );
$end_time = gmdate( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() );
$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 );
$expected = (object) array(
'total' => 1,
'pages' => 1,
'page_no' => 1,
'data' => array(
0 => array(
'order_id' => $order->get_id(),
'parent_id' => 0,
'status' => 'completed',
'net_total' => 65.0,
'total_sales' => 75.0,
'num_items_sold' => 5,
'customer_id' => $data->data[0]['customer_id'], // Not under test.
'customer_type' => 'new',
'date_created' => $data->data[0]['date_created'], // Not under test.
'date_created_gmt' => $data->data[0]['date_created_gmt'], // Not under test.
'extended_info' => array(
'products' => array(
array(
'id' => $variation->get_id(),
'name' => $variation->get_name(),
'quantity' => 4,
),
array(
'id' => $simple_product->get_id(),
'name' => $simple_product->get_name(),
'quantity' => 1,
),
),
'coupons' => array(),
'customer' => $data->data[0]['extended_info']['customer'], // Not under test.
),
),
),
);
$this->assertEquals( $expected, $data );
}
}