diff --git a/includes/updates/woocommerce-update-2.4.php b/includes/updates/woocommerce-update-2.4.php index a2d980a31af..0449a508d0c 100644 --- a/includes/updates/woocommerce-update-2.4.php +++ b/includes/updates/woocommerce-update-2.4.php @@ -12,5 +12,37 @@ if ( ! defined( 'ABSPATH' ) ) { exit; } +global $wpdb; + // Maintain the old coupon logic for upgrades -update_option( 'woocommerce_calc_discounts_sequentially', 'yes' ); \ No newline at end of file +update_option( 'woocommerce_calc_discounts_sequentially', 'yes' ); + +// Update fully refunded orders to ensure they have a refund line item so reports add up +$refunded_orders = get_posts( array( + 'posts_per_page' => -1, + 'post_type' => 'shop_order', + 'post_status' => array( 'wc-refunded' ) +) ); + +foreach ( $refunded_orders as $refunded_order ) { + $order_total = get_post_meta( $refunded_order->ID, '_order_total', true ); + $refunded_total = $wpdb->get_var( $wpdb->prepare( " + SELECT SUM( postmeta.meta_value ) + FROM $wpdb->postmeta AS postmeta + INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d ) + WHERE postmeta.meta_key = '_refund_amount' + AND postmeta.post_id = posts.ID + ", $refunded_order->ID ) ); + + if ( $order_total > $refunded_total ) { + $refund = wc_create_refund( array( + 'amount' => $order_total - $refunded_total, + 'reason' => __( 'Order Fully Refunded', 'woocommerce' ), + 'order_id' => $refunded_order->ID, + 'line_items' => array(), + 'date' => $refunded_order->post_modified + ) ); + } +} + +wc_delete_shop_order_transients(); \ No newline at end of file diff --git a/includes/wc-order-functions.php b/includes/wc-order-functions.php index 69ba3490972..5e8dc92e302 100644 --- a/includes/wc-order-functions.php +++ b/includes/wc-order-functions.php @@ -597,7 +597,8 @@ function wc_create_refund( $args = array() ) { 'reason' => null, 'order_id' => 0, 'refund_id' => 0, - 'line_items' => array() + 'line_items' => array(), + 'date' => current_time( 'mysql', 0 ) ); $args = wp_parse_args( $args, $default_args ); @@ -615,6 +616,7 @@ function wc_create_refund( $args = array() ) { $refund_data['post_password'] = uniqid( 'refund_' ); $refund_data['post_parent'] = absint( $args['order_id'] ); $refund_data['post_title'] = sprintf( __( 'Refund – %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) ); + $refund_data['post_date'] = $args['date']; } if ( ! is_null( $args['reason'] ) ) { @@ -757,7 +759,6 @@ function wc_get_payment_gateway_by_order( $order ) { */ function wc_order_fully_refunded( $order_id ) { $order = wc_get_order( $order_id ); - $order_items = $order->get_items(); $max_refund = wc_format_decimal( $order->get_total() - $order->get_total_refunded() ); if ( ! $max_refund ) {