Refund report changes
1. Ensure refund order_total is negative and reflects the full amount, not just line items 2. Ensure transients are cleared when doing refunds 3. sales-reports order types so we can exclude refunds from pure ‘sales’ reporting 4. Tweak placement of refunds in legend. Closes #6411
This commit is contained in:
parent
ef28c43862
commit
700369c386
|
@ -1487,10 +1487,7 @@ abstract class WC_Abstract_Order {
|
|||
* @return string
|
||||
*/
|
||||
public function get_order_currency() {
|
||||
|
||||
$currency = $this->order_currency;
|
||||
|
||||
return apply_filters( 'woocommerce_get_order_currency', $currency, $this );
|
||||
return apply_filters( 'woocommerce_get_order_currency', $this->order_currency, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1499,8 +1496,7 @@ abstract class WC_Abstract_Order {
|
|||
* @return string
|
||||
*/
|
||||
public function get_formatted_order_total() {
|
||||
|
||||
$formatted_total = wc_price( $this->order_total , array('currency' => $this->get_order_currency()));
|
||||
$formatted_total = wc_price( $this->get_total(), array( 'currency' => $this->get_order_currency() ) );
|
||||
|
||||
return apply_filters( 'woocommerce_get_formatted_order_total', $formatted_total, $this );
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'name' => 'total_shipping'
|
||||
)
|
||||
),
|
||||
'order_types' => wc_get_order_types( 'sales-reports' ),
|
||||
'filter_range' => true
|
||||
) );
|
||||
|
||||
|
@ -95,6 +96,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
)
|
||||
),
|
||||
'query_type' => 'get_var',
|
||||
'order_types' => array( 'shop_order_refund' ),
|
||||
'filter_range' => true
|
||||
) );
|
||||
|
||||
|
@ -135,18 +137,18 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'highlight_series' => 0
|
||||
);
|
||||
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s charged for shipping', 'woocommerce' ), '<strong>' . wc_price( $total_shipping ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['shipping_amount'],
|
||||
'highlight_series' => 5
|
||||
);
|
||||
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s in refunds', 'woocommerce' ), '<strong>' . wc_price( $total_refunds ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['refund_amount'],
|
||||
'highlight_series' => 4
|
||||
);
|
||||
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s charged for shipping', 'woocommerce' ), '<strong>' . wc_price( $total_shipping ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['shipping_amount'],
|
||||
'highlight_series' => 5
|
||||
);
|
||||
|
||||
$legend[] = array(
|
||||
'title' => sprintf( __( '%s worth of coupons used', 'woocommerce' ), '<strong>' . wc_price( $total_coupons ) . '</strong>' ),
|
||||
'color' => $this->chart_colours['coupon_amount'],
|
||||
|
@ -175,7 +177,7 @@ class WC_Report_Sales_By_Date extends WC_Admin_Report {
|
|||
'item_count' => '#d4d9dc',
|
||||
'coupon_amount' => '#e67e22',
|
||||
'shipping_amount' => '#1abc9c',
|
||||
'refund_amount' => '#800000'
|
||||
'refund_amount' => '#c0392b'
|
||||
);
|
||||
|
||||
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
|
||||
|
|
|
@ -1921,6 +1921,7 @@ class WC_AJAX {
|
|||
$refund_id = absint( $_POST['refund_id'] );
|
||||
|
||||
if ( $refund_id && 'shop_order_refund' === get_post_type( $refund_id ) ) {
|
||||
wc_delete_shop_order_transients( wp_get_post_parent_id( $refund_id ) );
|
||||
wp_delete_post( $refund_id );
|
||||
}
|
||||
|
||||
|
|
|
@ -308,16 +308,18 @@ class WC_Post_types {
|
|||
'shop_order_refund',
|
||||
apply_filters( 'woocommerce_register_post_type_shop_order_refund',
|
||||
array(
|
||||
'label' => __( 'Refunds', 'woocommerce' ),
|
||||
'capability_type' => 'shop_order',
|
||||
'public' => false,
|
||||
'hierarchical' => false,
|
||||
'supports' => false,
|
||||
'exclude_from_orders_screen' => false,
|
||||
'add_order_meta_boxes' => false,
|
||||
'exclude_from_order_count' => true,
|
||||
'exclude_from_order_views' => false,
|
||||
'class_name' => 'WC_Order_Refund'
|
||||
'label' => __( 'Refunds', 'woocommerce' ),
|
||||
'capability_type' => 'shop_order',
|
||||
'public' => false,
|
||||
'hierarchical' => false,
|
||||
'supports' => false,
|
||||
'exclude_from_orders_screen' => false,
|
||||
'add_order_meta_boxes' => false,
|
||||
'exclude_from_order_count' => true,
|
||||
'exclude_from_order_views' => false,
|
||||
'exclude_from_order_reports' => false,
|
||||
'exclude_from_order_sales_reports' => true,
|
||||
'class_name' => 'WC_Order_Refund'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
|
|
@ -124,6 +124,13 @@ function wc_get_order_types( $for = '' ) {
|
|||
}
|
||||
}
|
||||
break;
|
||||
case 'sales-reports' :
|
||||
foreach ( $wc_order_types as $type => $args ) {
|
||||
if ( ! $args['exclude_from_order_sales_reports'] ) {
|
||||
$order_types[] = $type;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default :
|
||||
$order_types = array_keys( $wc_order_types );
|
||||
break;
|
||||
|
@ -161,6 +168,7 @@ function wc_get_order_type( $type ) {
|
|||
* - exclude_from_order_views (bool) Whether or not this order type is visible by customers when
|
||||
* viewing orders e.g. on the my account page.
|
||||
* - exclude_from_order_reports (bool) Whether or not to exclude this type from core reports.
|
||||
* - exclude_from_order_sales_reports (bool) Whether or not to exclude this type from core sales reports.
|
||||
*
|
||||
* @since 2.2
|
||||
* @see register_post_type for $args used in that function
|
||||
|
@ -186,12 +194,13 @@ function wc_register_order_type( $type, $args = array() ) {
|
|||
|
||||
// Register for WC usage
|
||||
$order_type_args = array(
|
||||
'exclude_from_orders_screen' => false,
|
||||
'add_order_meta_boxes' => true,
|
||||
'exclude_from_order_count' => false,
|
||||
'exclude_from_order_views' => false,
|
||||
'exclude_from_order_reports' => false,
|
||||
'class_name' => 'WC_Order'
|
||||
'exclude_from_orders_screen' => false,
|
||||
'add_order_meta_boxes' => true,
|
||||
'exclude_from_order_count' => false,
|
||||
'exclude_from_order_views' => false,
|
||||
'exclude_from_order_reports' => false,
|
||||
'exclude_from_order_sales_reports' => false,
|
||||
'class_name' => 'WC_Order'
|
||||
);
|
||||
|
||||
$args = array_intersect_key( $args, $order_type_args );
|
||||
|
@ -596,11 +605,13 @@ function wc_create_refund( $args = array() ) {
|
|||
// Default refund meta data
|
||||
update_post_meta( $refund_id, '_refund_amount', wc_format_decimal( $args['amount'] ) );
|
||||
|
||||
// Get refund object
|
||||
$refund = wc_get_order( $refund_id );
|
||||
|
||||
// Negative line items
|
||||
if ( sizeof( $args['line_items'] ) > 0 ) {
|
||||
$order = wc_get_order( $args['order_id'] );
|
||||
$order_items = $order->get_items( array( 'line_item', 'fee', 'shipping' ) );
|
||||
$refund = wc_get_order( $refund_id );
|
||||
|
||||
foreach ( $args['line_items'] as $refund_item_id => $refund_item ) {
|
||||
if ( isset( $order_items[ $refund_item_id ] ) ) {
|
||||
|
@ -615,7 +626,7 @@ function wc_create_refund( $args = array() ) {
|
|||
|
||||
switch ( $order_items[ $refund_item_id ]['type'] ) {
|
||||
case 'line_item' :
|
||||
$args = array(
|
||||
$line_item_args = array(
|
||||
'totals' => array(
|
||||
'subtotal' => wc_format_refund_total( $refund_item['refund_total'] ),
|
||||
'total' => wc_format_refund_total( $refund_item['refund_total'] ),
|
||||
|
@ -624,7 +635,7 @@ function wc_create_refund( $args = array() ) {
|
|||
'tax_data' => array( 'total' => array_map( 'wc_format_refund_total', $refund_item['refund_tax'] ), 'subtotal' => array_map( 'wc_format_refund_total', $refund_item['refund_tax'] ) )
|
||||
)
|
||||
);
|
||||
$new_item_id = $refund->add_product( $order->get_product_from_item( $order_items[ $refund_item_id ] ), isset( $refund_item['qty'] ) ? $refund_item['qty'] : 0, $args );
|
||||
$new_item_id = $refund->add_product( $order->get_product_from_item( $order_items[ $refund_item_id ] ), isset( $refund_item['qty'] ) ? $refund_item['qty'] : 0, $line_item_args );
|
||||
wc_add_order_item_meta( $new_item_id, '_refunded_item_id', $refund_item_id );
|
||||
break;
|
||||
case 'shipping' :
|
||||
|
@ -653,10 +664,17 @@ function wc_create_refund( $args = array() ) {
|
|||
}
|
||||
}
|
||||
$refund->update_taxes();
|
||||
$refund->calculate_totals( false );
|
||||
}
|
||||
|
||||
$refund->calculate_totals( false );
|
||||
|
||||
// Set total to total refunded which may vary from order items
|
||||
$refund->set_total( wc_format_decimal( $args['amount'] ) * -1, 'total' );
|
||||
}
|
||||
|
||||
// Clear transients
|
||||
wc_delete_shop_order_transients( $args['order_id'] );
|
||||
|
||||
return new WC_Order_Refund( $refund_id );
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue