added method to save all order taxes

This commit is contained in:
claudiosmweb 2014-07-20 20:53:43 -03:00
parent 20f4509a93
commit 2d7e1cc51a
2 changed files with 152 additions and 94 deletions

View File

@ -32,14 +32,18 @@ if ( 'yes' == get_option( 'woocommerce_calc_taxes' ) ) {
<?php
if ( 'yes' == get_option( 'woocommerce_calc_taxes' ) ) :
foreach ( $order_taxes as $tax_item ) :
foreach ( $order_taxes as $tax_id => $tax_item ) :
$tax_class = wc_get_tax_class_by_tax_id( $tax_item['rate_id'] );
$tax_class_name = isset( $classes_options[ $tax_class ] ) ? $classes_options[ $tax_class ] : __( 'Tax', 'woocommerce' );
?>
<th class="line_tax"><?php echo esc_attr( $tax_class_name ); ?> <span class="tips" data-tip="<?php
<th class="line_tax">
<span><?php echo esc_attr( $tax_class_name ); ?> </span>
<span class="tips" data-tip="<?php
echo esc_attr( $tax_item['label'] . ' (' . $tax_item['name'] . ')' );
?>">[?]</span></th>
?>">[?]</span>
<input type="hidden" name="order_taxes[<?php echo $tax_id; ?>]" value="<?php echo esc_attr( $tax_item['rate_id'] ); ?>">
</th>
<?php
endforeach;

View File

@ -210,6 +210,7 @@ function wc_save_order_items( $order_id, $items ) {
// Order items + fees
$subtotal = 0;
$total = 0;
$taxes = array( 'items' => array(), 'shipping' => array() );
if ( isset( $items['order_item_id'] ) ) {
@ -257,6 +258,7 @@ function wc_save_order_items( $order_id, $items ) {
$tax_data_total = array_map( 'wc_format_decimal', $line_tax[ $item_id ] );
$tax_data_subtotal = array_map( 'wc_format_decimal', $line_subtotal_tax[ $item_id ] );
wc_update_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $tax_data_total, 'subtotal' => $tax_data_subtotal ) );
$taxes['items'][] = $tax_data_total;
// Total up
$subtotal += wc_format_decimal( $line_subtotal[ $item_id ] );
@ -315,10 +317,58 @@ function wc_save_order_items( $order_id, $items ) {
wc_update_order_item_meta( $item_id, 'cost', $cost );
wc_update_order_item_meta( $item_id, 'taxes', $ship_taxes );
$taxes['shipping'][] = $ship_taxes;
$order_shipping += $cost;
}
}
// Taxes
$order_taxes = isset( $items['order_taxes'] ) ? $items['order_taxes'] : array();
$taxes_items = array();
$taxes_shipping = array();
$total_tax = 0;
$total_shipping_tax = 0;
// Sum items taxes
foreach ( $taxes['items'] as $rates ) {
foreach ( $rates as $id => $value ) {
if ( isset( $taxes_items[ $id ] ) ) {
$taxes_items[ $id ] += $value;
} else {
$taxes_items[ $id ] = $value;
}
}
}
// Sum shipping taxes
foreach ( $taxes['shipping'] as $rates ) {
foreach ( $rates as $id => $value ) {
if ( isset( $taxes_shipping[ $id ] ) ) {
$taxes_shipping[ $id ] += $value;
} else {
$taxes_shipping[ $id ] = $value;
}
}
}
// Update order taxes
foreach ( $order_taxes as $item_id => $rate_id ) {
if ( isset( $taxes_items[ $rate_id ] ) ) {
$_total = wc_format_decimal( $taxes_items[ $rate_id ] );
wc_update_order_item_meta( $item_id, 'tax_amount', $_total );
$total_tax += $_total;
}
if ( isset( $taxes_shipping[ $rate_id ] ) ) {
$_total = wc_format_decimal( $taxes_shipping[ $rate_id ] );
wc_update_order_item_meta( $item_id, 'shipping_tax_amount', $_total );
$total_shipping_tax += $_total;
}
}
// Update order shipping total
update_post_meta( $order_id, '_order_shipping', $order_shipping );
@ -329,6 +379,10 @@ function wc_save_order_items( $order_id, $items ) {
update_post_meta( $order_id, '_order_discount', wc_format_decimal( $items['_order_discount'] ) );
update_post_meta( $order_id, '_order_total', wc_format_decimal( $items['_order_total'] ) );
// Update tax
update_post_meta( $order_id, '_order_tax', wc_format_decimal( $total_tax ) );
update_post_meta( $order_id, '_order_shipping_tax', wc_format_decimal( $total_shipping_tax ) );
// Remove old values
delete_post_meta( $order_id, '_shipping_method' );
delete_post_meta( $order_id, '_shipping_method_title' );