From abaa8f47d8759db448a561c4fa3f253511544915 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 6 Oct 2015 14:43:33 +0100 Subject: [PATCH] Moved refund hooks to avoid emails after API refund failure --- includes/class-wc-ajax.php | 17 ++++++++++++----- includes/class-wc-order.php | 16 ++++++++++++++++ includes/wc-order-functions.php | 16 ---------------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index fd10bcae761..788fb12d271 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -2218,7 +2218,7 @@ class WC_AJAX { 'amount' => $refund_amount, 'reason' => $refund_reason, 'order_id' => $order_id, - 'line_items' => $line_items + 'line_items' => $line_items, ) ); if ( is_wp_error( $refund ) ) { @@ -2260,10 +2260,18 @@ class WC_AJAX { } } - // Check if items are refunded fully - $max_remaining_items = absint( $order->get_item_count() - $order->get_item_count_refunded() ); + // Trigger notifications and status changes + if ( $order->get_remaining_refund_amount() > 0 || $order->get_remaining_refund_items() > 0 ) { + /** + * woocommerce_order_partially_refunded + * + * @since 2.4.0 + * Note: 3rd arg was added in err. Kept for bw compat. 2.4.3 + */ + do_action( 'woocommerce_order_partially_refunded', $order_id, $refund->id, $refund->id ); + } else { + do_action( 'woocommerce_order_fully_refunded', $order_id, $refund->id ); - if ( $refund_amount == $max_refund && 0 === $max_remaining_items ) { $order->update_status( apply_filters( 'woocommerce_order_fully_refunded_status', 'refunded', $order_id, $refund->id ) ); $response_data['status'] = 'fully_refunded'; } @@ -2272,7 +2280,6 @@ class WC_AJAX { // Clear transients wc_delete_shop_order_transients( $order_id ); - wp_send_json_success( $response_data ); } catch ( Exception $e ) { diff --git a/includes/class-wc-order.php b/includes/class-wc-order.php index 5bd843847d9..16da3ddf923 100644 --- a/includes/class-wc-order.php +++ b/includes/class-wc-order.php @@ -280,4 +280,20 @@ class WC_Order extends WC_Abstract_Order { return $total; } + + /** + * How much money is left to refund? + * @return string + */ + public function get_remaining_refund_amount() { + return wc_format_decimal( $this->get_total() - $this->get_total_refunded() ); + } + + /** + * How many items are left to refund? + * @return int + */ + public function get_remaining_refund_items() { + return absint( $this->get_item_count() - $this->get_item_count_refunded() ); + } } diff --git a/includes/wc-order-functions.php b/includes/wc-order-functions.php index 593bb7d6553..1e1363a4000 100644 --- a/includes/wc-order-functions.php +++ b/includes/wc-order-functions.php @@ -717,22 +717,6 @@ function wc_create_refund( $args = array() ) { // Set total to total refunded which may vary from order items $refund->set_total( wc_format_decimal( $args['amount'] ) * -1, 'total' ); - // Figure out if this is just a partial refund - $max_remaining_refund = wc_format_decimal( $order->get_total() - $order->get_total_refunded() ); - $max_remaining_items = absint( $order->get_item_count() - $order->get_item_count_refunded() ); - - if ( $max_remaining_refund > 0 || $max_remaining_items > 0 ) { - /** - * woocommerce_order_partially_refunded - * - * @since 2.4.0 - * Note: 3rd arg was added in err. Kept for bw compat. 2.4.3 - */ - do_action( 'woocommerce_order_partially_refunded', $args['order_id'], $refund_id, $refund_id ); - } else { - do_action( 'woocommerce_order_fully_refunded', $args['order_id'], $refund_id ); - } - do_action( 'woocommerce_refund_created', $refund_id, $args ); }