Calc totals/tax rows rework
This commit is contained in:
parent
c2e1258d04
commit
7e5d2ac174
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
?>
|
||||
<div class="tax_row" data-order_item_id="<?php echo $item_id; ?>">
|
||||
<p class="first">
|
||||
<label><?php _e( 'Tax Label:', 'woocommerce' ) ?></label>
|
||||
<input type="text" name="order_taxes_label[<?php echo $item_id; ?>]" placeholder="<?php echo $woocommerce->countries->tax_or_vat(); ?>" value="<?php if ( isset( $item['name'] ) ) echo esc_attr( $item['name'] ); ?>" />
|
||||
<input type="hidden" name="order_taxes_id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item_id ); ?>" />
|
||||
</p>
|
||||
<p class="last">
|
||||
<label><?php _e( 'Compound:', 'woocommerce' ) ?>
|
||||
<input type="checkbox" name="order_taxes_compound[<?php echo $item_id; ?>]" <?php if ( isset( $item['compound'] ) ) checked( $item['compound'], 1 ); ?> /></label>
|
||||
</p>
|
||||
<p class="first">
|
||||
<label><?php _e( 'Sales Tax:', 'woocommerce' ) ?></label>
|
||||
<input type="text" name="order_taxes_amount[<?php echo $item_id; ?>]" placeholder="0.00" value="<?php if ( isset( $item['tax_amount'] ) ) echo esc_attr( $item['tax_amount'] ); ?>" />
|
||||
</p>
|
||||
<p class="last">
|
||||
<label><?php _e( 'Shipping Tax:', 'woocommerce' ) ?></label>
|
||||
<input type="text" name="order_taxes_shipping_amount[<?php echo $item_id; ?>]" placeholder="0.00" value="<?php if ( isset( $item['shipping_tax_amount'] ) ) echo esc_attr( $item['shipping_tax_amount'] ); ?>" />
|
||||
</p>
|
||||
<a href="#" class="delete_tax_row">×</a>
|
||||
<div class="clear"></div>
|
||||
</div>
|
|
@ -459,8 +459,13 @@ function woocommerce_order_actions_meta_box($post) {
|
|||
* @param mixed $post
|
||||
* @return void
|
||||
*/
|
||||
function woocommerce_order_totals_meta_box($post) {
|
||||
global $woocommerce;
|
||||
function woocommerce_order_totals_meta_box( $post ) {
|
||||
global $woocommerce, $theorder;
|
||||
|
||||
if ( ! is_object( $theorder ) )
|
||||
$theorder = new WC_Order( $post->ID );
|
||||
|
||||
$order = $theorder;
|
||||
|
||||
$data = get_post_custom( $post->ID );
|
||||
?>
|
||||
|
@ -536,42 +541,16 @@ function woocommerce_order_totals_meta_box($post) {
|
|||
<?php do_action( 'woocommerce_admin_order_totals_after_shipping', $post->ID ) ?>
|
||||
<div class="clear"></div>
|
||||
</div>
|
||||
<div class="totals_group">
|
||||
<div class="totals_group tax_rows_group">
|
||||
<h4><?php _e( 'Tax Rows', 'woocommerce' ); ?></h4>
|
||||
<div id="tax_rows" class="total_rows">
|
||||
<?php
|
||||
$loop = 0;
|
||||
$taxes = isset( $data['_order_taxes'][0] ) ? maybe_unserialize( $data['_order_taxes'][0] ) : '';
|
||||
if ( is_array( $taxes ) && sizeof( $taxes ) > 0 ) {
|
||||
foreach ( $taxes as $tax ) {
|
||||
?>
|
||||
<div class="total_row">
|
||||
<p class="first">
|
||||
<label><?php _e( 'Tax Label:', 'woocommerce' ); ?></label>
|
||||
<input type="text" name="_order_taxes_label[<?php echo $loop; ?>]" placeholder="<?php echo $woocommerce->countries->tax_or_vat(); ?>" value="<?php echo esc_attr( $tax['label'] ); ?>" />
|
||||
</p>
|
||||
<p class="last">
|
||||
<label><?php _e( 'Compound:', 'woocommerce' ); ?>
|
||||
<input type="checkbox" name="_order_taxes_compound[<?php echo $loop; ?>]" <?php checked( $tax['compound'], 1 ); ?> /></label>
|
||||
</p>
|
||||
<p class="first">
|
||||
<label><?php _e( 'Sales Tax:', 'woocommerce' ); ?></label>
|
||||
<input type="text" name="_order_taxes_cart[<?php echo $loop; ?>]" placeholder="0.00" value="<?php echo esc_attr( $tax['cart_tax'] ); ?>" />
|
||||
</p>
|
||||
<p class="last">
|
||||
<label><?php _e( 'Shipping Tax:', 'woocommerce' ); ?></label>
|
||||
<input type="text" name="_order_taxes_shipping[<?php echo $loop; ?>]" placeholder="0.00" value="<?php echo esc_attr( $tax['shipping_tax'] ); ?>" />
|
||||
</p>
|
||||
<a href="#" class="delete_tax_row">×</a>
|
||||
<div class="clear"></div>
|
||||
</div>
|
||||
<?php
|
||||
$loop++;
|
||||
}
|
||||
foreach ( $order->get_taxes() as $item_id => $item ) {
|
||||
include( 'order-tax-html.php' );
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<h4><a href="#" class="add_tax_row tips" data-tip="<?php _e( 'These rows contain taxes for this order. This allows you to display multiple or compound taxes rather than a single total.', 'woocommerce' ); ?>"><?php _e( '+ Add tax row', 'woocommerce' ); ?> [?]</a></a></h4>
|
||||
<h4><a href="#" class="add_tax_row"><?php _e( '+ Add tax row', 'woocommerce' ); ?> <span class="tips" data-tip="<?php _e( 'These rows contain taxes for this order. This allows you to display multiple or compound taxes rather than a single total.', 'woocommerce' ); ?>">[?]</span></a></a></h4>
|
||||
<div class="clear"></div>
|
||||
</div>
|
||||
<div class="totals_group">
|
||||
|
@ -737,42 +716,42 @@ function woocommerce_process_shop_order_meta( $post_id, $post ) {
|
|||
|
||||
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_date = %s WHERE ID = %s", date_i18n( 'Y-m-d H:i:s', $date ), $post_id ) );
|
||||
|
||||
|
||||
// Tax rows
|
||||
$order_taxes = array();
|
||||
if ( isset( $_POST['order_taxes_id'] ) ) {
|
||||
|
||||
$get_values = array( 'order_taxes_id', 'order_taxes_label', 'order_taxes_compound', 'order_taxes_amount', 'order_taxes_shipping_amount' );
|
||||
|
||||
if ( isset( $_POST['_order_taxes_label'] ) ) {
|
||||
foreach( $get_values as $value )
|
||||
$$value = isset( $_POST[ $value ] ) ? $_POST[ $value ] : array();
|
||||
|
||||
$order_taxes_label = $_POST['_order_taxes_label'];
|
||||
$order_taxes_compound = isset( $_POST['_order_taxes_compound'] ) ? $_POST['_order_taxes_compound'] : array();
|
||||
$order_taxes_cart = $_POST['_order_taxes_cart'];
|
||||
$order_taxes_shipping = $_POST['_order_taxes_shipping'];
|
||||
$order_taxes_label_count = sizeof( $order_taxes_label );
|
||||
foreach( $order_taxes_id as $item_id ) {
|
||||
|
||||
$item_id = absint( $item_id );
|
||||
|
||||
for ( $i = 0; $i < $order_taxes_label_count; $i ++ ) {
|
||||
|
||||
// Add to array if the tax amount is set
|
||||
if ( ! $order_taxes_cart[ $i ] && ! $order_taxes_shipping[ $i ] )
|
||||
continue;
|
||||
|
||||
if ( ! $order_taxes_label[ $i ] )
|
||||
$order_taxes_label[ $i ] = $woocommerce->countries->tax_or_vat();
|
||||
|
||||
if ( isset( $order_taxes_compound[ $i ] ) )
|
||||
$is_compound = 1;
|
||||
else
|
||||
$is_compound = 0;
|
||||
|
||||
$order_taxes[] = array(
|
||||
'label' => esc_attr( $order_taxes_label[ $i ] ),
|
||||
'compound' => $is_compound,
|
||||
'cart_tax' => esc_attr( $order_taxes_cart[ $i ] ),
|
||||
'shipping_tax' => esc_attr( $order_taxes_shipping[ $i ] )
|
||||
);
|
||||
if ( ! $order_taxes_label[ $item_id ] )
|
||||
$order_taxes_label[ $item_id ] = $woocommerce->countries->tax_or_vat();
|
||||
|
||||
if ( isset( $order_taxes_label[ $item_id ] ) )
|
||||
$wpdb->update(
|
||||
$wpdb->prefix . "woocommerce_order_items",
|
||||
array( 'order_item_name' => woocommerce_clean( $order_taxes_label[ $item_id ] ) ),
|
||||
array( 'order_item_id' => $item_id ),
|
||||
array( '%s' ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
if ( isset( $order_taxes_compound[ $item_id ] ) )
|
||||
woocommerce_update_order_item_meta( $item_id, 'compound', isset( $order_taxes_compound[ $item_id ] ) ? 1 : 0 );
|
||||
|
||||
if ( isset( $order_taxes_amount[ $item_id ] ) )
|
||||
woocommerce_update_order_item_meta( $item_id, 'tax_amount', woocommerce_clean( $order_taxes_amount[ $item_id ] ) );
|
||||
|
||||
if ( isset( $order_taxes_shipping_amount[ $item_id ] ) )
|
||||
woocommerce_update_order_item_meta( $item_id, 'shipping_tax_amount', woocommerce_clean( $order_taxes_shipping_amount[ $item_id ] ) );
|
||||
}
|
||||
}
|
||||
|
||||
update_post_meta( $post_id, '_order_taxes', $order_taxes );
|
||||
|
||||
|
||||
// Order items + fees
|
||||
if ( isset( $_POST['order_item_id'] ) ) {
|
||||
|
|
|
@ -414,22 +414,15 @@ function woocommerce_admin_scripts() {
|
|||
'visible_label' => __( 'Visible on the product page', 'woocommerce' ),
|
||||
'used_for_variations_label' => __( 'Used for variations', 'woocommerce' ),
|
||||
'new_attribute_prompt' => __( 'Enter a name for the new attribute term:', 'woocommerce' ),
|
||||
'calc_totals' => __( 'Calculate totals based on order items, discount amount, and shipping? Note, you will need to (optionally) calculate tax rows and cart discounts manually.', 'woocommerce' ),
|
||||
'calc_totals' => __( 'Calculate totals based on order items, discounts, and shipping?', 'woocommerce' ),
|
||||
'calc_line_taxes' => __( 'Calculate line taxes? This will calculate taxes based on the customers country. If no billing/shipping is set it will use the store base country.', 'woocommerce' ),
|
||||
'copy_billing' => __( 'Copy billing information to shipping information? This will remove any currently entered shipping information.', 'woocommerce' ),
|
||||
'load_billing' => __( 'Load the customer\'s billing information? This will remove any currently entered billing information.', 'woocommerce' ),
|
||||
'load_shipping' => __( 'Load the customer\'s shipping information? This will remove any currently entered shipping information.', 'woocommerce' ),
|
||||
'featured_label' => __( 'Featured', 'woocommerce' ),
|
||||
'tax_or_vat' => $woocommerce->countries->tax_or_vat(),
|
||||
'prices_include_tax' => esc_attr( get_option('woocommerce_prices_include_tax') ),
|
||||
'round_at_subtotal' => esc_attr( get_option( 'woocommerce_tax_round_at_subtotal' ) ),
|
||||
'meta_name' => __( 'Meta Name', 'woocommerce' ),
|
||||
'meta_value' => __( 'Meta Value', 'woocommerce' ),
|
||||
'no_customer_selected' => __( 'No customer selected', 'woocommerce' ),
|
||||
'tax_label' => __( 'Tax Label:', 'woocommerce' ),
|
||||
'compound_label' => __( 'Compound:', 'woocommerce' ),
|
||||
'cart_tax_label' => __( 'Cart Tax:', 'woocommerce' ),
|
||||
'shipping_tax_label' => __( 'Shipping Tax:', 'woocommerce' ),
|
||||
'plugin_url' => $woocommerce->plugin_url(),
|
||||
'ajax_url' => admin_url('admin-ajax.php'),
|
||||
'order_item_nonce' => wp_create_nonce("order-item"),
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -610,10 +610,10 @@ table.wc_status_table {
|
|||
border-style: dotted;
|
||||
}
|
||||
}
|
||||
.total_rows {
|
||||
#tax_rows {
|
||||
margin: 6px 0 0;
|
||||
}
|
||||
.total_row {
|
||||
.tax_row {
|
||||
margin:0 0 9px;
|
||||
border:1px solid #DFDFDF;
|
||||
border-right-width: 2px;
|
||||
|
|
|
@ -323,6 +323,7 @@ jQuery( function($){
|
|||
|
||||
var data = {
|
||||
action: 'woocommerce_calc_line_taxes',
|
||||
order_id: woocommerce_writepanel_params.post_id,
|
||||
items: calculate_items,
|
||||
shipping: accounting.unformat( $('#_order_shipping').val() ),
|
||||
country: country,
|
||||
|
@ -342,6 +343,7 @@ jQuery( function($){
|
|||
|
||||
$row.find('input.line_tax').val( result['item_taxes'][ item_id ]['line_tax'] ).change();
|
||||
$row.find('input.line_subtotal_tax').val( result['item_taxes'][ item_id ]['line_subtotal_tax'] ).change();
|
||||
$('#tax_rows').empty().append( result['tax_row_html'] );
|
||||
} );
|
||||
|
||||
$('#_order_shipping_tax').val( result['shipping_tax'] ).change();
|
||||
|
@ -354,9 +356,9 @@ jQuery( function($){
|
|||
}
|
||||
return false;
|
||||
}).hover(function() {
|
||||
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax, #_order_shipping_tax, #_order_tax').css('background-color', '#d8c8d2');
|
||||
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax, #_order_shipping_tax, #_order_tax, .tax_rows_group').css('background-color', '#e3d2dd');
|
||||
}, function() {
|
||||
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax, #_order_shipping_tax, #_order_tax').css('background-color', '');
|
||||
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax, #_order_shipping_tax, #_order_tax, .tax_rows_group').css('background-color', '');
|
||||
});
|
||||
|
||||
|
||||
|
@ -405,6 +407,16 @@ jQuery( function($){
|
|||
cart_tax = cart_tax + parseFloat( line_tax );
|
||||
});
|
||||
|
||||
// Tax
|
||||
if (woocommerce_writepanel_params.round_at_subtotal=='yes') {
|
||||
cart_tax = accounting.toFixed( cart_tax, 2 );
|
||||
}
|
||||
|
||||
// Cart discount
|
||||
var cart_discount = ( line_subtotals + line_subtotal_taxes ) - ( line_totals + cart_tax );
|
||||
if ( cart_discount < 0 ) cart_discount = 0;
|
||||
cart_discount = accounting.toFixed( cart_discount, 2 );
|
||||
|
||||
$('#order_items_list tr.fee').each(function(){
|
||||
var line_total = accounting.unformat( $(this).find('input.line_total').val() );
|
||||
var line_tax = accounting.unformat( $(this).find('input.line_tax').val() );
|
||||
|
@ -426,11 +438,6 @@ jQuery( function($){
|
|||
cart_tax = accounting.toFixed( cart_tax, 2 );
|
||||
}
|
||||
|
||||
// Cart discount
|
||||
var cart_discount = ( (line_subtotals + line_subtotal_taxes) - (line_totals + cart_tax) );
|
||||
if ( cart_discount < 0 ) cart_discount = 0;
|
||||
cart_discount = accounting.toFixed( cart_discount, 2 );
|
||||
|
||||
// Total
|
||||
var order_total = line_totals + cart_tax + order_shipping + order_shipping_tax - order_discount;
|
||||
order_total = accounting.toFixed( order_total, 2 );
|
||||
|
@ -441,8 +448,6 @@ jQuery( function($){
|
|||
$('#_order_tax').val( cart_tax ).change();
|
||||
$('#_order_total').val( order_total ).change();
|
||||
|
||||
// Since we currently cannot calc shipping from the backend, ditch the rows. They must be manually calculated.
|
||||
$('#tax_rows').empty();
|
||||
$('#woocommerce-order-totals').unblock();
|
||||
|
||||
} else {
|
||||
|
@ -450,7 +455,7 @@ jQuery( function($){
|
|||
}
|
||||
return false;
|
||||
}).hover(function() {
|
||||
$('#woocommerce-order-totals .calculated').css('background-color', '#d8c8d2');
|
||||
$('#woocommerce-order-totals .calculated').css('background-color', '#e3d2dd');
|
||||
}, function() {
|
||||
$('#woocommerce-order-totals .calculated').css('background-color', '');
|
||||
});
|
||||
|
@ -810,38 +815,53 @@ jQuery( function($){
|
|||
return false;
|
||||
});
|
||||
|
||||
// Add a tax row
|
||||
$('a.add_tax_row').live('click', function(){
|
||||
|
||||
var size = $('#tax_rows .tax_row').size();
|
||||
var data = {
|
||||
order_id: woocommerce_writepanel_params.post_id,
|
||||
action: 'woocommerce_add_line_tax',
|
||||
security: woocommerce_writepanel_params.calc_totals_nonce
|
||||
};
|
||||
|
||||
$('#tax_rows').append('<div class="tax_row">\
|
||||
<p class="first">\
|
||||
<label>' + woocommerce_writepanel_params.tax_label + '</label>\
|
||||
<input type="text" name="_order_taxes_label[' + size + ']" placeholder="' + woocommerce_writepanel_params.tax_or_vat + '" />\
|
||||
</p>\
|
||||
<p class="last">\
|
||||
<label>' + woocommerce_writepanel_params.compound_label + '\
|
||||
<input type="checkbox" name="_order_taxes_compound[' + size + ']" /></label>\
|
||||
</p>\
|
||||
<p class="first">\
|
||||
<label>' + woocommerce_writepanel_params.cart_tax_label + '</label>\
|
||||
<input type="text" name="_order_taxes_cart[' + size + ']" placeholder="0.00" />\
|
||||
</p>\
|
||||
<p class="last">\
|
||||
<label>' + woocommerce_writepanel_params.shipping_tax_label + '</label>\
|
||||
<input type="text" name="_order_taxes_shipping[' + size + ']" placeholder="0.00" />\
|
||||
</p>\
|
||||
<a href="#" class="delete_tax_row">×</a>\
|
||||
<div class="clear"></div>\
|
||||
</div>');
|
||||
$('#tax_rows').closest('.totals_group').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
|
||||
|
||||
$.ajax({
|
||||
url: woocommerce_writepanel_params.ajax_url,
|
||||
data: data,
|
||||
type: 'POST',
|
||||
success: function( response ) {
|
||||
$('#tax_rows').append( response ).closest('.totals_group').unblock();
|
||||
}
|
||||
});
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
|
||||
// Delete a tax row
|
||||
$('a.delete_tax_row').live('click', function(){
|
||||
$tax_row = $(this).closest('.tax_row');
|
||||
$tax_row.find('input').val('');
|
||||
$tax_row.hide();
|
||||
|
||||
var tax_row_id = $tax_row.attr( 'data-order_item_id' )
|
||||
|
||||
var data = {
|
||||
tax_row_id: tax_row_id,
|
||||
action: 'woocommerce_remove_line_tax',
|
||||
security: woocommerce_writepanel_params.calc_totals_nonce
|
||||
};
|
||||
|
||||
$('#tax_rows').closest('.totals_group').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
|
||||
|
||||
$.ajax({
|
||||
url: woocommerce_writepanel_params.ajax_url,
|
||||
data: data,
|
||||
type: 'POST',
|
||||
success: function( response ) {
|
||||
$tax_row.remove();
|
||||
$('#tax_rows').closest('.totals_group').unblock();
|
||||
}
|
||||
});
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -438,6 +438,16 @@ class WC_Order {
|
|||
function get_fees() {
|
||||
return $this->get_items( 'fee' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of taxes within this order.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function get_taxes() {
|
||||
return $this->get_items( 'tax' );
|
||||
}
|
||||
|
||||
/**
|
||||
* has_meta function for order items.
|
||||
|
@ -466,18 +476,6 @@ class WC_Order {
|
|||
return get_metadata( 'order_item', $order_item_id, $key, $single );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of taxes for this order.
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
function get_taxes() {
|
||||
if ( ! $this->taxes )
|
||||
$this->taxes = isset( $this->order_custom_fields['_order_taxes'][0] ) ? maybe_unserialize( $this->order_custom_fields['_order_taxes'][0] ) : array();
|
||||
return $this->taxes;
|
||||
}
|
||||
|
||||
|
||||
/** Total Getters *******************************************************/
|
||||
|
||||
|
@ -752,16 +750,16 @@ class WC_Order {
|
|||
// Remove non-compound taxes
|
||||
foreach ( $this->get_taxes() as $tax ) :
|
||||
|
||||
if (isset($tax['compound']) && $tax['compound']) continue;
|
||||
if ( ! empty( $tax['compound'] ) ) continue;
|
||||
|
||||
$subtotal = $subtotal + $tax['cart_tax'] + $tax['shipping_tax'];
|
||||
$subtotal = $subtotal + $tax['tax_amount'] + $tax['shipping_tax_amount'];
|
||||
|
||||
endforeach;
|
||||
|
||||
// Remove discounts
|
||||
$subtotal = $subtotal - $this->get_cart_discount();
|
||||
|
||||
$subtotal = woocommerce_price($subtotal);
|
||||
$subtotal = woocommerce_price( $subtotal );
|
||||
|
||||
endif;
|
||||
|
||||
|
@ -918,12 +916,12 @@ class WC_Order {
|
|||
continue;
|
||||
}
|
||||
|
||||
if ( ( $tax[ 'cart_tax' ] + $tax[ 'shipping_tax' ] ) == 0 )
|
||||
if ( ( $tax[ 'tax_amount' ] + $tax[ 'shipping_tax_amount' ] ) == 0 )
|
||||
continue;
|
||||
|
||||
$total_rows[ sanitize_title( $tax[ 'label' ] ) ] = array(
|
||||
'label' => $tax[ 'label' ] . ':',
|
||||
'value' => woocommerce_price( ( $tax[ 'cart_tax' ] + $tax[ 'shipping_tax' ] ) )
|
||||
$total_rows[ sanitize_title( $tax[ 'name' ] ) ] = array(
|
||||
'label' => $tax[ 'name' ] . ':',
|
||||
'value' => woocommerce_price( ( $tax[ 'tax_amount' ] + $tax[ 'shipping_tax_amount' ] ) )
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -940,12 +938,12 @@ class WC_Order {
|
|||
if ( ! $tax[ 'compound' ] )
|
||||
continue;
|
||||
|
||||
if ( ( $tax[ 'cart_tax' ] + $tax[ 'shipping_tax' ] ) == 0 )
|
||||
if ( ( $tax[ 'tax_amount' ] + $tax[ 'shipping_tax_amount' ] ) == 0 )
|
||||
continue;
|
||||
|
||||
$total_rows[ sanitize_title( $tax[ 'label' ] ) ] = array(
|
||||
'label' => $tax[ 'label' ] . ':',
|
||||
'value' => woocommerce_price( ( $tax[ 'cart_tax' ] + $tax[ 'shipping_tax' ] ) )
|
||||
$total_rows[ sanitize_title( $tax[ 'name' ] ) ] = array(
|
||||
'label' => $tax[ 'name' ] . ':',
|
||||
'value' => woocommerce_price( ( $tax[ 'tax_amount' ] + $tax[ 'shipping_tax_amount' ] ) )
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
@ -986,10 +984,10 @@ class WC_Order {
|
|||
|
||||
foreach ( $this->get_taxes() as $tax ) {
|
||||
|
||||
if ( ( $tax[ 'cart_tax' ] + $tax[ 'shipping_tax' ] ) == 0 )
|
||||
if ( ( $tax[ 'tax_amount' ] + $tax[ 'shipping_tax_amount' ] ) == 0 )
|
||||
continue;
|
||||
|
||||
$tax_string_array[] = sprintf( '%s %s', woocommerce_price( ( $tax[ 'cart_tax' ] + $tax[ 'shipping_tax' ] ) ), $tax[ 'label' ] );
|
||||
$tax_string_array[] = sprintf( '%s %s', woocommerce_price( ( $tax[ 'tax_amount' ] + $tax[ 'shipping_tax_amount' ] ) ), $tax[ 'name' ] );
|
||||
}
|
||||
|
||||
} else {
|
||||
|
|
|
@ -193,6 +193,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
|
|||
* Tweak - Taxes - tweaked display of tax when using inclusive pricing to avoid confusion.
|
||||
* Tweak - More granular capabilities for admin/shop manager covering products, orders and coupons.
|
||||
* Tweak - Added some calculations to the order page when manually entering rows. Also added accounting.js for more accurate rounding of floats.
|
||||
* Tweak - Order page can now calculate tax rows for you.
|
||||
* Tweak - Display tax/discount total for reference on orders
|
||||
* Tweak - Humanised order email subjects/headings
|
||||
* Tweak - Cleaned up the tax settings.
|
||||
|
|
|
@ -1095,14 +1095,15 @@ add_action( 'wp_ajax_woocommerce_remove_order_item_meta', 'woocommerce_ajax_remo
|
|||
* @return void
|
||||
*/
|
||||
function woocommerce_calc_line_taxes() {
|
||||
global $woocommerce;
|
||||
global $woocommerce, $wpdb;
|
||||
|
||||
check_ajax_referer( 'calc-totals', 'security' );
|
||||
|
||||
$tax = new WC_Tax();
|
||||
|
||||
$taxes = $tax_rows = $item_taxes = $shipping_taxes = array();
|
||||
|
||||
|
||||
$order_id = absint( $_POST['order_id'] );
|
||||
$country = strtoupper( esc_attr( $_POST['country'] ) );
|
||||
$state = strtoupper( esc_attr( $_POST['state'] ) );
|
||||
$postcode = strtoupper( esc_attr( $_POST['postcode'] ) );
|
||||
|
@ -1186,27 +1187,48 @@ function woocommerce_calc_line_taxes() {
|
|||
$shipping_taxes = $tax->calc_shipping_tax( $shipping, $matched_tax_rates );
|
||||
$shipping_tax = rtrim( rtrim( number_format( array_sum( $shipping_taxes ), 2, '.', '' ), '0' ), '.' );
|
||||
|
||||
// Remove old tax rows
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = 'tax' )", $order_id ) );
|
||||
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = 'tax'", $order_id ) );
|
||||
|
||||
// Now merge to keep tax rows
|
||||
ob_start();
|
||||
|
||||
foreach ( array_keys( $taxes + $shipping_taxes ) as $key ) {
|
||||
|
||||
$is_compound = $tax->is_compound( $key ) ? 1 : 0;
|
||||
$item = array();
|
||||
$item['name'] = $tax->get_rate_label( $key );
|
||||
$item['compound'] = $tax->is_compound( $key ) ? 1 : 0;
|
||||
$item['tax_amount'] = woocommerce_format_total( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
|
||||
$item['shipping_tax_amount'] = woocommerce_format_total( isset( $shipping_taxes[ $key ] ) ? $shipping_taxes[ $key ] : 0 );
|
||||
|
||||
$cart_tax = isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0;
|
||||
$shipping_tax = isset( $shipping_taxes[ $key ] ) ? $shipping_taxes[ $key ] : 0;
|
||||
if ( ! $item['name'] )
|
||||
$item['name'] = $woocommerce->countries->tax_or_vat();
|
||||
|
||||
$tax_rows[] = array(
|
||||
'label' => $tax->get_rate_label( $key ),
|
||||
'compound' => $is_compound,
|
||||
'cart_tax' => woocommerce_format_total( $cart_tax ),
|
||||
'shipping_tax' => woocommerce_format_total( $shipping_tax )
|
||||
);
|
||||
// Add line item
|
||||
$item_id = woocommerce_add_order_item( $order_id, array(
|
||||
'order_item_name' => $item['name'],
|
||||
'order_item_type' => 'tax'
|
||||
) );
|
||||
|
||||
// Add line item meta
|
||||
if ( $item_id ) {
|
||||
woocommerce_add_order_item_meta( $item_id, 'compound', $item['compound'] );
|
||||
woocommerce_add_order_item_meta( $item_id, 'tax_amount', $item['tax_amount'] );
|
||||
woocommerce_add_order_item_meta( $item_id, 'shipping_tax_amount', $item['shipping_tax_amount'] );
|
||||
}
|
||||
|
||||
include( 'admin/post-types/writepanels/order-tax-html.php' );
|
||||
}
|
||||
|
||||
$tax_row_html = ob_get_clean();
|
||||
|
||||
// Return
|
||||
echo json_encode( array(
|
||||
'item_taxes' => $item_taxes,
|
||||
'shipping_tax' => $shipping_tax,
|
||||
'tax_rows' => $tax_rows
|
||||
'tax_row_html' => $tax_row_html
|
||||
) );
|
||||
|
||||
// Quit out
|
||||
|
@ -1215,6 +1237,61 @@ function woocommerce_calc_line_taxes() {
|
|||
|
||||
add_action('wp_ajax_woocommerce_calc_line_taxes', 'woocommerce_calc_line_taxes');
|
||||
|
||||
/**
|
||||
* woocommerce_add_line_tax function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function woocommerce_add_line_tax() {
|
||||
global $woocommerce;
|
||||
|
||||
check_ajax_referer( 'calc-totals', 'security' );
|
||||
|
||||
$order_id = absint( $_POST['order_id'] );
|
||||
$order = new WC_Order( $order_id );
|
||||
|
||||
// Add line item
|
||||
$item_id = woocommerce_add_order_item( $order_id, array(
|
||||
'order_item_name' => '',
|
||||
'order_item_type' => 'tax'
|
||||
) );
|
||||
|
||||
// Add line item meta
|
||||
if ( $item_id ) {
|
||||
woocommerce_add_order_item_meta( $item_id, 'compound', '' );
|
||||
woocommerce_add_order_item_meta( $item_id, 'tax_amount', '' );
|
||||
woocommerce_add_order_item_meta( $item_id, 'shipping_tax_amount', '' );
|
||||
}
|
||||
|
||||
include( 'admin/post-types/writepanels/order-tax-html.php' );
|
||||
|
||||
// Quit out
|
||||
die();
|
||||
}
|
||||
|
||||
add_action('wp_ajax_woocommerce_add_line_tax', 'woocommerce_add_line_tax');
|
||||
|
||||
/**
|
||||
* woocommerce_add_line_tax function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function woocommerce_remove_line_tax() {
|
||||
global $woocommerce;
|
||||
|
||||
check_ajax_referer( 'calc-totals', 'security' );
|
||||
|
||||
$tax_row_id = absint( $_POST['tax_row_id'] );
|
||||
|
||||
woocommerce_delete_order_item( $tax_row_id );
|
||||
|
||||
// Quit out
|
||||
die();
|
||||
}
|
||||
|
||||
add_action('wp_ajax_woocommerce_remove_line_tax', 'woocommerce_remove_line_tax');
|
||||
|
||||
/**
|
||||
* Add order note via ajax
|
||||
|
|
Loading…
Reference in New Issue