diff --git a/includes/abstracts/abstract-wc-order.php b/includes/abstracts/abstract-wc-order.php index 23aa228923a..336ddec7383 100644 --- a/includes/abstracts/abstract-wc-order.php +++ b/includes/abstracts/abstract-wc-order.php @@ -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; diff --git a/includes/admin/meta-boxes/views/html-order-shipping.php b/includes/admin/meta-boxes/views/html-order-shipping.php index e48ef4225fe..9cc6f9c6ae3 100644 --- a/includes/admin/meta-boxes/views/html-order-shipping.php +++ b/includes/admin/meta-boxes/views/html-order-shipping.php @@ -13,8 +13,8 @@ if ( ! defined( 'ABSPATH' ) ) { @@ -50,7 +50,7 @@ if ( ! defined( 'ABSPATH' ) ) { diff --git a/includes/admin/wc-admin-functions.php b/includes/admin/wc-admin-functions.php index cc6538abae4..29bf210c7ab 100644 --- a/includes/admin/wc-admin-functions.php +++ b/includes/admin/wc-admin-functions.php @@ -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; } } diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 4698cfb588e..3bfcdd8d0cb 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -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' ); diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index fb25729272d..5fc13170a8c 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.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 );