improved the saving the new tax data
This commit is contained in:
parent
2cfebdc6c5
commit
5152457d9d
|
@ -260,6 +260,10 @@ abstract class WC_Abstract_Order {
|
|||
wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $fee->amount ) );
|
||||
wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $fee->tax ) );
|
||||
|
||||
// Save tax data - Since 2.2
|
||||
$tax_data = array_map( 'wc_format_decimal', $fee->tax_data );
|
||||
wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $tax_data ) );
|
||||
|
||||
do_action( 'woocommerce_order_add_fee', $this->id, $item_id, $fee );
|
||||
|
||||
return $item_id;
|
||||
|
|
|
@ -13,8 +13,8 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
<?php echo ! empty( $item['name'] ) ? esc_html( $item['name'] ) : __( 'Shipping', 'woocommerce' ); ?>
|
||||
</div>
|
||||
<div class="edit" style="display: none;">
|
||||
<input type="text" placeholder="<?php _e( 'Shipping Name', 'woocommerce' ); ?>" name="shipping_method_title[<?php echo $item_id ? $item_id : 'new][]'; ?>]" value="<?php echo ( isset( $item['name'] ) ) ? esc_attr( $item['name'] ) : ''; ?>" />
|
||||
<select name="shipping_method[<?php echo $item_id ? $item_id : 'new][]'; ?>]">
|
||||
<input type="text" placeholder="<?php _e( 'Shipping Name', 'woocommerce' ); ?>" name="shipping_method_title[<?php echo $item_id; ?>]" value="<?php echo ( isset( $item['name'] ) ) ? esc_attr( $item['name'] ) : ''; ?>" />
|
||||
<select name="shipping_method[<?php echo $item_id; ?>]">
|
||||
<optgroup label="<?php _e( 'Shipping Method', 'woocommerce' ); ?>">
|
||||
<option value=""><?php _e( 'N/A', 'woocommerce' ); ?></option>
|
||||
<?php
|
||||
|
@ -39,7 +39,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
?>
|
||||
</optgroup>
|
||||
</select>
|
||||
<input type="hidden" name="shipping_method_id[<?php echo $item_id ? $item_id : 'new][]'; ?>]" value="<?php echo esc_attr( $item_id ); ?>" />
|
||||
<input type="hidden" name="shipping_method_id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item_id ); ?>" />
|
||||
</div>
|
||||
</td>
|
||||
|
||||
|
@ -50,7 +50,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
<?php echo ( isset( $item['cost'] ) ) ? wc_price( wc_round_tax_total( $item['cost'] ) ) : ''; ?>
|
||||
</div>
|
||||
<div class="edit" style="display: none;">
|
||||
<label><?php _e( 'Total', 'woocommerce' ); ?>: <input type="text" name="shipping_cost[<?php echo $item_id ? $item_id : 'new][]'; ?>]" placeholder="<?php echo wc_format_localized_price( 0 ); ?>" value="<?php echo ( isset( $item['cost'] ) ) ? esc_attr( wc_format_localized_price( $item['cost'] ) ) : ''; ?>" class="line_total wc_input_price" /></label>
|
||||
<label><?php _e( 'Total', 'woocommerce' ); ?>: <input type="text" name="shipping_cost[<?php echo $item_id; ?>]" placeholder="<?php echo wc_format_localized_price( 0 ); ?>" value="<?php echo ( isset( $item['cost'] ) ) ? esc_attr( wc_format_localized_price( $item['cost'] ) ) : ''; ?>" class="line_total wc_input_price" /></label>
|
||||
</div>
|
||||
</td>
|
||||
|
||||
|
|
|
@ -297,51 +297,25 @@ function wc_save_order_items( $order_id, $items ) {
|
|||
}
|
||||
|
||||
foreach ( $shipping_method_id as $item_id => $value ) {
|
||||
$item_id = absint( $item_id );
|
||||
$method_id = wc_clean( $shipping_method[ $item_id ] );
|
||||
$method_title = wc_clean( $shipping_method_title[ $item_id ] );
|
||||
$cost = wc_format_decimal( $shipping_cost[ $item_id ] );
|
||||
$ship_taxes = isset( $shipping_taxes[ $item_id ] ) ? array_map( 'wc_format_decimal', $shipping_taxes[ $item_id ] ) : array();
|
||||
|
||||
if ( 'new' == $item_id ) {
|
||||
$wpdb->update(
|
||||
$wpdb->prefix . 'woocommerce_order_items',
|
||||
array( 'order_item_name' => $method_title ),
|
||||
array( 'order_item_id' => $item_id ),
|
||||
array( '%s' ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
foreach ( $value as $new_key => $new_value ) {
|
||||
$method_id = wc_clean( $shipping_method[ $item_id ][ $new_key ] );
|
||||
$method_title = wc_clean( $shipping_method_title[ $item_id ][ $new_key ] );
|
||||
$cost = wc_format_decimal( $shipping_cost[ $item_id ][ $new_key ] );
|
||||
$ship_taxes = array_map( 'wc_format_decimal', $shipping_taxes[ $item_id ] );
|
||||
wc_update_order_item_meta( $item_id, 'method_id', $method_id );
|
||||
wc_update_order_item_meta( $item_id, 'cost', $cost );
|
||||
wc_update_order_item_meta( $item_id, 'taxes', $ship_taxes );
|
||||
|
||||
$new_id = wc_add_order_item( $order_id, array(
|
||||
'order_item_name' => $method_title,
|
||||
'order_item_type' => 'shipping'
|
||||
) );
|
||||
|
||||
if ( $new_id ) {
|
||||
wc_add_order_item_meta( $new_id, 'method_id', $method_id );
|
||||
wc_add_order_item_meta( $new_id, 'cost', $cost );
|
||||
wc_add_order_item_meta( $new_id, 'taxes', $ship_taxes );
|
||||
}
|
||||
|
||||
$order_shipping += $cost;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
$item_id = absint( $item_id );
|
||||
$method_id = wc_clean( $shipping_method[ $item_id ] );
|
||||
$method_title = wc_clean( $shipping_method_title[ $item_id ] );
|
||||
$cost = wc_format_decimal( $shipping_cost[ $item_id ] );
|
||||
$ship_taxes = array_map( 'wc_format_decimal', $shipping_taxes[ $item_id ] );
|
||||
|
||||
$wpdb->update(
|
||||
$wpdb->prefix . 'woocommerce_order_items',
|
||||
array( 'order_item_name' => $method_title ),
|
||||
array( 'order_item_id' => $item_id ),
|
||||
array( '%s' ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
wc_update_order_item_meta( $item_id, 'method_id', $method_id );
|
||||
wc_update_order_item_meta( $item_id, 'cost', $cost );
|
||||
wc_update_order_item_meta( $item_id, 'taxes', $ship_taxes );
|
||||
|
||||
$order_shipping += $cost;
|
||||
}
|
||||
$order_shipping += $cost;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -940,9 +940,10 @@ class WC_AJAX {
|
|||
die();
|
||||
}
|
||||
|
||||
$_product = get_product( $post->ID );
|
||||
$order = get_order( $order_id );
|
||||
$class = 'new_row';
|
||||
$_product = get_product( $post->ID );
|
||||
$order = get_order( $order_id );
|
||||
$order_taxes = $order->get_taxes();
|
||||
$class = 'new_row';
|
||||
|
||||
// Set values
|
||||
$item = array();
|
||||
|
@ -975,6 +976,9 @@ class WC_AJAX {
|
|||
wc_add_order_item_meta( $item_id, '_line_total', $item['line_total'] );
|
||||
wc_add_order_item_meta( $item_id, '_line_tax', $item['line_tax'] );
|
||||
|
||||
// Since 2.2
|
||||
wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => array(), 'subtotal' => array() ) );
|
||||
|
||||
// Store variation data in meta
|
||||
if ( $item['variation_data'] && is_array( $item['variation_data'] ) ) {
|
||||
foreach ( $item['variation_data'] as $key => $value ) {
|
||||
|
@ -1000,8 +1004,9 @@ class WC_AJAX {
|
|||
|
||||
check_ajax_referer( 'order-item', 'security' );
|
||||
|
||||
$order_id = absint( $_POST['order_id'] );
|
||||
$order = get_order( $order_id );
|
||||
$order_id = absint( $_POST['order_id'] );
|
||||
$order = get_order( $order_id );
|
||||
$order_taxes = $order->get_taxes();
|
||||
|
||||
// Add line item
|
||||
$item_id = wc_add_order_item( $order_id, array(
|
||||
|
@ -1014,6 +1019,9 @@ class WC_AJAX {
|
|||
wc_add_order_item_meta( $item_id, '_tax_class', '' );
|
||||
wc_add_order_item_meta( $item_id, '_line_total', '' );
|
||||
wc_add_order_item_meta( $item_id, '_line_tax', '' );
|
||||
|
||||
// Since 2.2
|
||||
wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => array() ) );
|
||||
}
|
||||
|
||||
include( 'admin/meta-boxes/views/html-order-fee.php' );
|
||||
|
@ -1031,6 +1039,7 @@ class WC_AJAX {
|
|||
|
||||
$order_id = absint( $_POST['order_id'] );
|
||||
$order = get_order( $order_id );
|
||||
$order_taxes = $order->get_taxes();
|
||||
$shipping_methods = WC()->shipping() ? WC()->shipping->load_shipping_methods() : array();
|
||||
|
||||
// Add line item
|
||||
|
@ -1043,6 +1052,7 @@ class WC_AJAX {
|
|||
if ( $item_id ) {
|
||||
wc_add_order_item_meta( $item_id, 'method_id', '' );
|
||||
wc_add_order_item_meta( $item_id, 'cost', '' );
|
||||
wc_add_order_item_meta( $item_id, 'taxes', array() );
|
||||
}
|
||||
|
||||
include( 'admin/meta-boxes/views/html-order-shipping.php' );
|
||||
|
|
|
@ -1879,14 +1879,15 @@ class WC_Cart {
|
|||
}
|
||||
}
|
||||
|
||||
$new_fee = new stdClass();
|
||||
$new_fee->id = $new_fee_id;
|
||||
$new_fee->name = esc_attr( $name );
|
||||
$new_fee->amount = (float) esc_attr( $amount );
|
||||
$new_fee->tax_class = $tax_class;
|
||||
$new_fee->taxable = $taxable ? true : false;
|
||||
$new_fee->tax = 0;
|
||||
$this->fees[] = $new_fee;
|
||||
$new_fee = new stdClass();
|
||||
$new_fee->id = $new_fee_id;
|
||||
$new_fee->name = esc_attr( $name );
|
||||
$new_fee->amount = (float) esc_attr( $amount );
|
||||
$new_fee->tax_class = $tax_class;
|
||||
$new_fee->taxable = $taxable ? true : false;
|
||||
$new_fee->tax = 0;
|
||||
$new_fee->tax_data = array();
|
||||
$this->fees[] = $new_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1921,6 +1922,9 @@ class WC_Cart {
|
|||
// Set the tax total for this fee
|
||||
$this->fees[ $fee_key ]->tax = array_sum( $fee_taxes );
|
||||
|
||||
// Set tax data - Since 2.2
|
||||
$this->fees[ $fee_key ]->tax_data = $fee_taxes;
|
||||
|
||||
// Tax rows - merge the totals we just got
|
||||
foreach ( array_keys( $this->taxes + $fee_taxes ) as $key ) {
|
||||
$this->taxes[ $key ] = ( isset( $fee_taxes[ $key ] ) ? $fee_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
|
||||
|
|
Loading…
Reference in New Issue