Add a dedicated hook for adding fees. Closes #3611. Fixes #3352

@coenjacobs we might want this in .14 for some extensions in audit.
This commit is contained in:
Mike Jolley 2013-08-13 17:19:20 +01:00
parent d021980c10
commit 64e1b091a0
1 changed files with 37 additions and 26 deletions

View File

@ -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 );
}
}
}
}
}
/*-----------------------------------------------------------------------------------*/