Take item qty into consideration when refunding orders

Fixes #8675
This commit is contained in:
Mike Jolley 2015-07-29 13:13:41 +01:00
parent b0ee9a8cc6
commit 7b2aec4791
5 changed files with 61 additions and 24 deletions

View File

@ -1180,36 +1180,27 @@ abstract class WC_Abstract_Order {
}
/**
* Gets order total - formatted for display.
*
* @param string $type
* Gets the count of order items of a certain type.
*
* @param string $item_type
* @return string
*/
public function get_item_count( $type = '' ) {
if ( empty( $type ) ) {
$type = array( 'line_item' );
public function get_item_count( $item_type = '' ) {
if ( empty( $item_type ) ) {
$item_type = array( 'line_item' );
}
if ( ! is_array( $item_type ) ) {
$item_type = array( $item_type );
}
if ( ! is_array( $type ) ) {
$type = array( $type );
}
$items = $this->get_items( $type );
$items = $this->get_items( $item_type );
$count = 0;
foreach ( $items as $item ) {
if ( ! empty( $item['qty'] ) ) {
$count += $item['qty'];
} else {
$count ++;
}
$count += empty( $item['qty'] ) ? 1 : $item['qty'];
}
return apply_filters( 'woocommerce_get_item_count', $count, $type, $this );
return apply_filters( 'woocommerce_get_item_count', $count, $item_type, $this );
}
/**

View File

@ -237,7 +237,7 @@ if ( wc_tax_enabled() ) {
<?php if ( wc_tax_enabled() && $order->is_editable() ) : ?>
<button type="button" class="button add-order-tax"><?php _e( 'Add Tax', 'woocommerce' ); ?></button>
<?php endif; ?>
<?php if ( ( $order->get_total() - $order->get_total_refunded() ) > 0 ) : ?>
<?php if ( 0 < $order->get_total() - $order->get_total_refunded() || 0 < absint( $order->get_item_count() - $order->get_item_count_refunded() ) ) : ?>
<button type="button" class="button refund-items"><?php _e( 'Refund', 'woocommerce' ); ?></button>
<?php endif; ?>
<?php
@ -261,7 +261,7 @@ if ( wc_tax_enabled() ) {
do_action( 'woocommerce_order_item_add_line_buttons', $order );
?>
</div>
<?php if ( ( $order->get_total() - $order->get_total_refunded() ) > 0 ) : ?>
<?php if ( 0 < $order->get_total() - $order->get_total_refunded() || 0 < absint( $order->get_item_count() - $order->get_item_count_refunded() ) ) : ?>
<div class="wc-order-data-row wc-order-refund-items" style="display: none;">
<table class="wc-order-totals">
<tr style="display:none;">

View File

@ -2241,7 +2241,10 @@ class WC_AJAX {
}
}
if ( $refund_amount == $max_refund ) {
// Check if items are refunded fully
$max_remaining_items = absint( $order->get_item_count() - $order->get_item_count_refunded() );
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';
}

View File

@ -140,6 +140,48 @@ class WC_Order extends WC_Abstract_Order {
return abs( $total );
}
/**
* Gets the count of order items of a certain type that have been refunded.
* @since 2.4.0
* @param string $item_type
* @return string
*/
public function get_item_count_refunded( $item_type = '' ) {
if ( empty( $item_type ) ) {
$item_type = array( 'line_item' );
}
if ( ! is_array( $item_type ) ) {
$item_type = array( $item_type );
}
$count = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
$count += empty( $refunded_item['qty'] ) ? 0 : $refunded_item['qty'];
}
}
return apply_filters( 'woocommerce_get_item_count_refunded', $count, $item_type, $this );
}
/**
* Get the total number of items refunded.
*
* @since 2.4.0
* @param int $item_id ID of the item we're checking
* @param string $item_type type of the item we're checking, if not a line_item
* @return integer
*/
public function get_total_qty_refunded( $item_type = 'line_item' ) {
$qty = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
$qty += $refunded_item['qty'];
}
}
return $qty;
}
/**
* Get the refunded amount for a line item
*

View File

@ -719,8 +719,9 @@ function wc_create_refund( $args = array() ) {
// 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 ) {
if ( $max_remaining_refund > 0 || $max_remaining_items > 0 ) {
do_action( 'woocommerce_order_partially_refunded', $args['order_id'], true, $refund_id );
}