From 1715e98b989aad4d4f4865e6fe084dd973c713a0 Mon Sep 17 00:00:00 2001 From: Joshua Flowers Date: Wed, 6 Mar 2019 14:25:59 +0800 Subject: [PATCH 1/3] Don't fetch extended attributes if product doesn't exist --- .../class-wc-admin-reports-products-data-store.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php index 4aee8834add..99f31210312 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php @@ -193,7 +193,13 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i foreach ( $products_data as $key => $product_data ) { $extended_info = new ArrayObject(); if ( $query_args['extended_info'] ) { - $product = wc_get_product( $product_data['product_id'] ); + $product = wc_get_product( $product_data['product_id'] ); + // Product was deleted. + if ( ! $product ) { + $products_data[ $key ]['extended_info']['name'] = __( '(Deleted)', 'wc-admin' ); + continue; + } + $extended_attributes = apply_filters( 'woocommerce_rest_reports_products_extended_attributes', $this->extended_attributes, $product_data ); foreach ( $extended_attributes as $extended_attribute ) { if ( 'variations' === $extended_attribute ) { From c6c71b0238e06c5ed9cae60f7f9527b7a5dc8159 Mon Sep 17 00:00:00 2001 From: Joshua Flowers Date: Wed, 6 Mar 2019 15:09:38 +0800 Subject: [PATCH 2/3] Delete product IDs from lookup table on delete --- ...s-wc-admin-reports-products-data-store.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php index 99f31210312..e775fdb3ae0 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php @@ -90,6 +90,7 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i */ public static function init() { add_action( 'woocommerce_reports_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 10 ); + add_action( 'delete_post', array( __CLASS__, 'delete_product_id' ) ); } /** @@ -474,4 +475,24 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i */ do_action( 'woocommerce_reports_delete_product', 0, $order_id ); } + + /** + * Deletes the product ID when a product is deleted. + * This keeps data consistent if it gets resynced at any point. + * + * @param int $post_id Post ID. + */ + public static function delete_product_id( $post_id ) { + global $wpdb; + + if ( 'product' !== get_post_type( $post_id ) ) { + return; + } + + $wpdb->update( + $wpdb->prefix . self::TABLE_NAME, + array( 'product_id' => 0 ), + array( 'product_id' => $post_id ) + ); + } } From b1890190c7b7246db1e336e923eb46b7b7eafaeb Mon Sep 17 00:00:00 2001 From: Joshua Flowers Date: Wed, 6 Mar 2019 16:03:54 +0800 Subject: [PATCH 3/3] Get product ID for line item from meta table instead of deleting --- ...s-wc-admin-reports-products-data-store.php | 25 ++----------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php index e775fdb3ae0..19f87a3ecd7 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php @@ -90,7 +90,6 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i */ public static function init() { add_action( 'woocommerce_reports_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 10 ); - add_action( 'delete_post', array( __CLASS__, 'delete_product_id' ) ); } /** @@ -403,8 +402,8 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i array( 'order_item_id' => $order_item_id, 'order_id' => $order->get_id(), - 'product_id' => $order_item->get_product_id( 'edit' ), - 'variation_id' => $order_item->get_variation_id( 'edit' ), + 'product_id' => wc_get_order_item_meta( $order_item_id, '_product_id' ), + 'variation_id' => wc_get_order_item_meta( $order_item_id, '_variation_id' ), 'customer_id' => ( 0 < $order->get_customer_id( 'edit' ) ) ? $order->get_customer_id( 'edit' ) : null, 'product_qty' => $product_qty, 'product_net_revenue' => $net_revenue, @@ -475,24 +474,4 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i */ do_action( 'woocommerce_reports_delete_product', 0, $order_id ); } - - /** - * Deletes the product ID when a product is deleted. - * This keeps data consistent if it gets resynced at any point. - * - * @param int $post_id Post ID. - */ - public static function delete_product_id( $post_id ) { - global $wpdb; - - if ( 'product' !== get_post_type( $post_id ) ) { - return; - } - - $wpdb->update( - $wpdb->prefix . self::TABLE_NAME, - array( 'product_id' => 0 ), - array( 'product_id' => $post_id ) - ); - } }