From bc39bdf64616d8eccea662e7bfd53aa6c7bf78e6 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Tue, 1 Sep 2020 08:22:36 -0400 Subject: [PATCH] Show full variation name instead of parent product in orders report. (https://github.com/woocommerce/woocommerce-admin/pull/5056) --- .../src/API/Reports/Orders/DataStore.php | 27 ++-- .../reports/class-wc-tests-reports-orders.php | 118 ++++++++++++++++++ 2 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php diff --git a/plugins/woocommerce-admin/src/API/Reports/Orders/DataStore.php b/plugins/woocommerce-admin/src/API/Reports/Orders/DataStore.php index 959ae736cb3..98e76ae16df 100644 --- a/plugins/woocommerce-admin/src/API/Reports/Orders/DataStore.php +++ b/plugins/woocommerce-admin/src/API/Reports/Orders/DataStore.php @@ -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. diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php new file mode 100644 index 00000000000..eb71423c417 --- /dev/null +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php @@ -0,0 +1,118 @@ +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 ); + } +}