From 64e1b091a0d12723e086fa23c2f668867dca14a4 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 13 Aug 2013 17:19:20 +0100 Subject: [PATCH] Add a dedicated hook for adding fees. Closes #3611. Fixes #3352 @coenjacobs we might want this in .14 for some extensions in audit. --- includes/class-wc-cart.php | 63 ++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index a39112e9eee..3bd6196f9b4 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -1598,28 +1598,15 @@ class WC_Cart { } } - // Add fees - foreach ( $this->get_fees() as $fee ) { - $this->fee_total += $fee->amount; - - if ( $fee->taxable ) { - // Get tax rates - $tax_rates = $this->tax->get_rates( $fee->tax_class ); - $fee_taxes = $this->tax->calc_tax( $fee->amount, $tax_rates, false ); - - // Store - $fee->tax = array_sum( $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 ); - } - } - } - // Only calculate the grand total + shipping if on the cart/checkout if ( is_checkout() || is_cart() || defined('WOOCOMMERCE_CHECKOUT') || defined('WOOCOMMERCE_CART') ) { + // Calculate the Shipping + $this->calculate_shipping(); + + // Trigger the fees API where developers can add fees to the cart + $this->calculate_fees(); + // Total up/round taxes if ( $this->round_at_subtotal ) { $this->tax_total = $this->tax->get_tax_total( $this->taxes ); @@ -1628,9 +1615,6 @@ class WC_Cart { $this->tax_total = array_sum( $this->taxes ); } - // Cart Shipping - $this->calculate_shipping(); - // Total up/round taxes for shipping if ( $this->round_at_subtotal ) { $this->shipping_tax_total = $this->tax->get_tax_total( $this->shipping_taxes ); @@ -2003,15 +1987,12 @@ class WC_Cart { /** * add_fee function. * - * @access public * @param mixed $name * @param mixed $amount * @param bool $taxable (default: false) * @param string $tax_class (default: '') - * @return void */ public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) { - if ( empty( $this->fees ) ) $this->fees = array(); @@ -2033,7 +2014,37 @@ class WC_Cart { * @return void */ public function get_fees() { - return (array) $this->fees; + return array_filter( (array) $this->fees ); + } + + /** + * Calculate fees + */ + public function calculate_fees() { + + // Fire an action where developers can add their fees + do_action( 'woocommerce_cart_calculate_fees', $this ); + + // If fees were added, total them and calculate tax + if ( $fees = $this->get_fees() ) { + foreach ( $fees as $fee ) { + $this->fee_total += $fee->amount; + + if ( $fee->taxable ) { + // Get tax rates + $tax_rates = $this->tax->get_rates( $fee->tax_class ); + $fee_taxes = $this->tax->calc_tax( $fee->amount, $tax_rates, false ); + + // Store + $fee->tax = array_sum( $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 ); + } + } + } + } } /*-----------------------------------------------------------------------------------*/