From 3be6664f0524e03ffb4b3c075d689f3dd90ebbf7 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 17 Feb 2014 15:11:50 +0000 Subject: [PATCH] Handle get_rate_code when no matching rate is found. Prevents notices. --- includes/class-wc-cart.php | 20 +++++++++++--------- includes/class-wc-checkout.php | 27 +++++++++++++++------------ includes/class-wc-tax.php | 4 ++++ 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 3f4e2d50761..ab7e3eea6f0 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -674,16 +674,18 @@ class WC_Cart { $code = $this->tax->get_rate_code( $key ); - if ( ! isset( $tax_totals[ $code ] ) ) { - $tax_totals[ $code ] = new stdClass(); - $tax_totals[ $code ]->amount = 0; - } + if ( $code ) { + if ( ! isset( $tax_totals[ $code ] ) ) { + $tax_totals[ $code ] = new stdClass(); + $tax_totals[ $code ]->amount = 0; + } - $tax_totals[ $code ]->tax_rate_id = $key; - $tax_totals[ $code ]->is_compound = $this->tax->is_compound( $key ); - $tax_totals[ $code ]->label = $this->tax->get_rate_label( $key ); - $tax_totals[ $code ]->amount += wc_round_tax_total( $tax ); - $tax_totals[ $code ]->formatted_amount = wc_price( wc_round_tax_total( $tax_totals[ $code ]->amount ) ); + $tax_totals[ $code ]->tax_rate_id = $key; + $tax_totals[ $code ]->is_compound = $this->tax->is_compound( $key ); + $tax_totals[ $code ]->label = $this->tax->get_rate_label( $key ); + $tax_totals[ $code ]->amount += wc_round_tax_total( $tax ); + $tax_totals[ $code ]->formatted_amount = wc_price( wc_round_tax_total( $tax_totals[ $code ]->amount ) ); + } } return apply_filters( 'woocommerce_cart_tax_totals', $tax_totals, $this ); diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index ab9f616f138..69f722812b3 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -327,19 +327,22 @@ class WC_Checkout { // Store tax rows foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $key ) { + $code = WC()->cart->tax->get_rate_code( $key ); + + if ( $code ) { + $item_id = wc_add_order_item( $order_id, array( + 'order_item_name' => $code, + 'order_item_type' => 'tax' + ) ); - $item_id = wc_add_order_item( $order_id, array( - 'order_item_name' => WC()->cart->tax->get_rate_code( $key ), - 'order_item_type' => 'tax' - ) ); - - // Add line item meta - if ( $item_id ) { - wc_add_order_item_meta( $item_id, 'rate_id', $key ); - wc_add_order_item_meta( $item_id, 'label', WC()->cart->tax->get_rate_label( $key ) ); - wc_add_order_item_meta( $item_id, 'compound', absint( WC()->cart->tax->is_compound( $key ) ? 1 : 0 ) ); - wc_add_order_item_meta( $item_id, 'tax_amount', wc_format_decimal( isset( WC()->cart->taxes[ $key ] ) ? WC()->cart->taxes[ $key ] : 0 ) ); - wc_add_order_item_meta( $item_id, 'shipping_tax_amount', wc_format_decimal( isset( WC()->cart->shipping_taxes[ $key ] ) ? WC()->cart->shipping_taxes[ $key ] : 0 ) ); + // Add line item meta + if ( $item_id ) { + wc_add_order_item_meta( $item_id, 'rate_id', $key ); + wc_add_order_item_meta( $item_id, 'label', WC()->cart->tax->get_rate_label( $key ) ); + wc_add_order_item_meta( $item_id, 'compound', absint( WC()->cart->tax->is_compound( $key ) ? 1 : 0 ) ); + wc_add_order_item_meta( $item_id, 'tax_amount', wc_format_decimal( isset( WC()->cart->taxes[ $key ] ) ? WC()->cart->taxes[ $key ] : 0 ) ); + wc_add_order_item_meta( $item_id, 'shipping_tax_amount', wc_format_decimal( isset( WC()->cart->shipping_taxes[ $key ] ) ? WC()->cart->shipping_taxes[ $key ] : 0 ) ); + } } } diff --git a/includes/class-wc-tax.php b/includes/class-wc-tax.php index b907eb2bc50..d6427688221 100644 --- a/includes/class-wc-tax.php +++ b/includes/class-wc-tax.php @@ -557,6 +557,10 @@ class WC_Tax { $rate = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $key ) ); + if ( ! $rate ) { + return ''; + } + $code = array(); $code[] = $rate->tax_rate_country;