remove deleted products, coupons from stats when order edited (https://github.com/woocommerce/woocommerce-admin/pull/3103)

* remove deleted products, couponsfrom stats when order edited

* sync with datastore refactor
This commit is contained in:
Ron Rennick 2019-11-12 15:07:14 -04:00 committed by GitHub
parent 9ea25daf81
commit b2b1934eea
2 changed files with 38 additions and 4 deletions

View File

@ -335,12 +335,16 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
return true;
}
$table_name = self::get_db_table_name();
$existing_items = $wpdb->get_col( $wpdb->prepare( "SELECT coupon_id FROM {$table_name} WHERE order_id = %d", $order_id ) );
$existing_items = array_flip( $existing_items );
$coupon_items = $order->get_items( 'coupon' );
$coupon_items_count = count( $coupon_items );
$num_updated = 0;
foreach ( $coupon_items as $coupon_item ) {
$coupon_id = wc_get_coupon_id_by_code( $coupon_item->get_code() );
unset( $existing_items[ $coupon_id ] );
if ( ! $coupon_id ) {
$coupon_items_count--;
@ -375,6 +379,19 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$num_updated += 2 === intval( $result ) ? 1 : intval( $result );
}
if ( ! empty( $existing_items ) ) {
$existing_items = array_flip( $existing_items );
$format = array_fill( 0, count( $existing_items ), '%d' );
$format = implode( ',', $format );
array_unshift( $existing_items, $order_id );
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$table_name} WHERE order_id = %d AND coupon_id in ({$format})",
$existing_items
)
);
}
return ( $coupon_items_count === $num_updated );
}

View File

@ -390,13 +390,17 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
return -1;
}
$order_items = $order->get_items();
$num_updated = 0;
$decimals = wc_get_price_decimals();
$round_tax = 'no' === get_option( 'woocommerce_tax_round_at_subtotal' );
$table_name = self::get_db_table_name();
$existing_items = $wpdb->get_col( $wpdb->prepare( "SELECT order_item_id FROM {$table_name} WHERE order_id = %d", $order_id ) );
$existing_items = array_flip( $existing_items );
$order_items = $order->get_items();
$num_updated = 0;
$decimals = wc_get_price_decimals();
$round_tax = 'no' === get_option( 'woocommerce_tax_round_at_subtotal' );
foreach ( $order_items as $order_item ) {
$order_item_id = $order_item->get_id();
unset( $existing_items[ $order_item_id ] );
$product_qty = $order_item->get_quantity( 'edit' );
$shipping_amount = $order->get_item_shipping_amount( $order_item );
$shipping_tax_amount = $order->get_item_shipping_tax_amount( $order_item );
@ -469,6 +473,19 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$num_updated += 2 === intval( $result ) ? 1 : intval( $result );
}
if ( ! empty( $existing_items ) ) {
$existing_items = array_flip( $existing_items );
$format = array_fill( 0, count( $existing_items ), '%d' );
$format = implode( ',', $format );
array_unshift( $existing_items, $order_id );
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$table_name} WHERE order_id = %d AND order_item_id in ({$format})",
$existing_items
)
);
}
return ( count( $order_items ) === $num_updated );
}