Merge pull request #13313 from woocommerce/fix-13264-2

Save existing rates rather than recreate all
This commit is contained in:
Claudio Sanches 2017-02-23 13:40:57 -03:00 committed by GitHub
commit c376173890
1 changed files with 17 additions and 4 deletions

View File

@ -1068,6 +1068,8 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
public function update_taxes() { public function update_taxes() {
$cart_taxes = array(); $cart_taxes = array();
$shipping_taxes = array(); $shipping_taxes = array();
$existing_taxes = $this->get_taxes();
$saved_rate_ids = array();
foreach ( $this->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) { foreach ( $this->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) {
$taxes = $item->get_taxes(); $taxes = $item->get_taxes();
@ -1083,11 +1085,22 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
} }
} }
// Remove old existing tax rows. foreach ( $existing_taxes as $tax ) {
$this->remove_order_items( 'tax' ); // Remove taxes which no longer exist for cart/shipping.
if ( ( ! array_key_exists( $tax->get_rate_id(), $cart_taxes ) && ! array_key_exists( $tax->get_rate_id(), $shipping_taxes ) ) || in_array( $tax->get_rate_id(), $saved_rate_ids ) ) {
$this->remove_item( $tax->get_id() );
continue;
}
$saved_rate_ids[] = $tax->get_rate_id();
$tax->set_tax_total( isset( $cart_taxes[ $tax->get_rate_id() ] ) ? $cart_taxes[ $tax->get_rate_id() ] : 0 );
$tax->set_shipping_tax_total( ! empty( $shipping_taxes[ $tax->get_rate_id() ] ) ? $shipping_taxes[ $tax->get_rate_id() ] : 0 );
$tax->save();
}
// Now merge to keep tax rows. $new_rate_ids = wp_parse_id_list( array_diff( array_keys( $cart_taxes + $shipping_taxes ), $saved_rate_ids ) );
foreach ( array_keys( $cart_taxes + $shipping_taxes ) as $tax_rate_id ) {
// New taxes.
foreach ( $new_rate_ids as $tax_rate_id ) {
$item = new WC_Order_Item_Tax(); $item = new WC_Order_Item_Tax();
$item->set_rate( $tax_rate_id ); $item->set_rate( $tax_rate_id );
$item->set_tax_total( isset( $cart_taxes[ $tax_rate_id ] ) ? $cart_taxes[ $tax_rate_id ] : 0 ); $item->set_tax_total( isset( $cart_taxes[ $tax_rate_id ] ) ? $cart_taxes[ $tax_rate_id ] : 0 );