Display taxes labels in order totals when order has refunded, closes #7229

This commit is contained in:
Claudio Sanches 2015-01-29 18:50:08 -02:00
parent e8bb986b59
commit def250e912
3 changed files with 118 additions and 35 deletions

View File

@ -1730,7 +1730,7 @@ abstract class WC_Abstract_Order {
if ( $fees = $this->get_fees() )
foreach( $fees as $id => $fee ) {
foreach ( $fees as $id => $fee ) {
if ( apply_filters( 'woocommerce_get_order_item_totals_excl_free_fees', $fee['line_total'] + $fee['line_tax'] == 0, $id ) ) {
continue;
@ -1769,7 +1769,7 @@ abstract class WC_Abstract_Order {
$total_rows['tax'] = array(
'label' => WC()->countries->tax_or_vat() . ':',
'value' => wc_price( $this->get_total_tax(), array('currency' => $this->get_order_currency()) )
'value' => wc_price( $this->get_total_tax(), array( 'currency' => $this->get_order_currency() ) )
);
}
}

View File

@ -58,6 +58,27 @@ class WC_Order extends WC_Abstract_Order {
return $total;
}
/**
* Get the total tax refunded
*
* @since 2.3
* @return float
*/
public function get_total_tax_refunded() {
global $wpdb;
$total = $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( order_itemmeta.meta_value )
FROM {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON ( order_items.order_id = posts.ID AND order_items.order_item_type = 'tax' )
WHERE order_itemmeta.order_item_id = order_items.order_item_id
AND order_itemmeta.meta_key IN ('tax_amount', 'shipping_tax_amount')
", $this->id ) );
return abs( $total );
}
/**
* Get the refunded amount for a line item
*
@ -135,4 +156,24 @@ class WC_Order extends WC_Abstract_Order {
}
return $total * -1;
}
/**
* Get total tax refunded by rate ID.
*
* @param int $rate_id
*
* @return float
*/
public function get_total_tax_refunded_by_rate_id( $rate_id ) {
$total = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( 'tax' ) as $refunded_item ) {
if ( isset( $refunded_item['rate_id'] ) && $refunded_item['rate_id'] == $rate_id ) {
$total += abs( $refunded_item['tax_amount'] ) + abs( $refunded_item['shipping_tax_amount'] );
}
}
}
return $total;
}
}

View File

@ -35,14 +35,15 @@ $order = wc_get_order( $order_id );
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="product-name">
<?php
if ( $_product && ! $_product->is_visible() )
if ( $_product && ! $_product->is_visible() ) {
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
else
} else {
echo apply_filters( 'woocommerce_order_item_name', sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ), $item );
}
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '&times; %s', $item['qty'] ) . '</strong>', $item );
// allow other plugins to add additional product information here
// Allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
$item_meta->display();
@ -62,7 +63,7 @@ $order = wc_get_order( $order_id );
echo '<br/>' . implode( '<br/>', $links );
}
// allow other plugins to add additional product information here
// Allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
?>
</td>
@ -90,16 +91,47 @@ $order = wc_get_order( $order_id );
<?php
$has_refund = false;
if ( $order->get_total_refunded() !== NULL ) {
if ( $total_refunded = $order->get_total_refunded() ) {
$has_refund = true;
}
if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $key => $total ) :
if ( $totals = $order->get_order_item_totals() ) {
foreach ( $totals as $key => $total ) {
$value = $total['value'];
// check for refund
// Check for refund
if ( $has_refund && $key === 'order_total' ) {
$value = '<del>' . strip_tags( $order->get_formatted_order_total() ) . '</del> <ins>' . wc_price( $order->get_total() - $order->get_total_refunded(), array( 'currency' => $order->get_order_currency() ) ) . '</ins>';
$refunded_tax_del = '';
$refunded_tax_ins = '';
// Tax for inclusive prices
if ( wc_tax_enabled() && 'incl' == $order->tax_display_cart ) {
$tax_del_array = array();
$tax_ins_array = array();
if ( 'itemized' == get_option( 'woocommerce_tax_total_display' ) ) {
foreach ( $order->get_tax_totals() as $code => $tax ) {
$tax_del_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
$tax_ins_array[] = sprintf( '%s %s', wc_price( $tax->amount - $order->get_total_tax_refunded_by_rate_id( $tax->rate_id ), array( 'currency' => $order->get_order_currency() ) ), $tax->label );
}
} else {
$tax_del_array[] = sprintf( '%s %s', wc_price( $order->get_total_tax(), array( 'currency' => $order->get_order_currency() ) ), WC()->countries->tax_or_vat() );
$tax_ins_array[] = sprintf( '%s %s', wc_price( $order->get_total_tax() - $order->get_total_tax_refunded(), array( 'currency' => $order->get_order_currency() ) ), WC()->countries->tax_or_vat() );
}
if ( ! empty( $tax_del_array ) ) {
$refunded_tax_del .= ' ' . sprintf( __( '(Includes %s)', 'woocommerce' ), implode( ', ', $tax_del_array ) );
}
if ( ! empty( $tax_ins_array ) ) {
$refunded_tax_ins .= ' ' . sprintf( __( '(Includes %s)', 'woocommerce' ), implode( ', ', $tax_ins_array ) );
}
}
$value = '<del>' . strip_tags( $order->get_formatted_order_total() ) . $refunded_tax_del . '</del> <ins>' . wc_price( $order->get_total() - $total_refunded, array( 'currency' => $order->get_order_currency() ) ) . $refunded_tax_ins . '</ins>';
}
?>
<tr>
@ -107,28 +139,25 @@ $order = wc_get_order( $order_id );
<td><?php echo $value; ?></td>
</tr>
<?php
endforeach;
}
}
// check for refund
if ( $has_refund ) {
?>
// Check for refund
if ( $has_refund ) { ?>
<tr>
<th scope="row"><?php _e( 'Refunded:', 'woocommerce' ); ?></th>
<td>-<?php echo wc_price( $order->get_total_refunded(), array( 'currency' => $order->get_order_currency() ) ); ?></td>
<td>-<?php echo wc_price( $total_refunded, array( 'currency' => $order->get_order_currency() ) ); ?></td>
</tr>
<?php
}
// Check for customer note
if ( '' != $order->customer_note ) {
?>
if ( '' != $order->customer_note ) { ?>
<tr>
<th scope="row"><?php _e( 'Note:', 'woocommerce' ); ?></th>
<td><?php echo wptexturize( $order->customer_note ); ?></td>
</tr>
<?php
}
?>
<?php } ?>
</tfoot>
</table>
@ -139,8 +168,13 @@ $order = wc_get_order( $order_id );
</header>
<table class="shop_table shop_table_responsive customer_details">
<?php
if ( $order->billing_email ) echo '<tr><th>' . __( 'Email:', 'woocommerce' ) . '</th><td data-title="' . __( 'Email', 'woocommerce' ) . '">' . $order->billing_email . '</td></tr>';
if ( $order->billing_phone ) echo '<tr><th>' . __( 'Telephone:', 'woocommerce' ) . '</th><td data-title="' . __( 'Telephone', 'woocommerce' ) . '">' . $order->billing_phone . '</td></tr>';
if ( $order->billing_email ) {
echo '<tr><th>' . __( 'Email:', 'woocommerce' ) . '</th><td data-title="' . __( 'Email', 'woocommerce' ) . '">' . $order->billing_email . '</td></tr>';
}
if ( $order->billing_phone ) {
echo '<tr><th>' . __( 'Telephone:', 'woocommerce' ) . '</th><td data-title="' . __( 'Telephone', 'woocommerce' ) . '">' . $order->billing_phone . '</td></tr>';
}
// Additional customer details hook
do_action( 'woocommerce_order_details_after_customer_details', $order );
@ -160,7 +194,11 @@ $order = wc_get_order( $order_id );
</header>
<address>
<?php
if ( ! $order->get_formatted_billing_address() ) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_billing_address();
if ( ! $order->get_formatted_billing_address() ) {
_e( 'N/A', 'woocommerce' );
} else {
echo $order->get_formatted_billing_address();
}
?>
</address>
@ -175,7 +213,11 @@ $order = wc_get_order( $order_id );
</header>
<address>
<?php
if ( ! $order->get_formatted_shipping_address() ) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_shipping_address();
if ( ! $order->get_formatted_shipping_address() ) {
_e( 'N/A', 'woocommerce' );
} else {
echo $order->get_formatted_shipping_address();
}
?>
</address>