php - How can I add a custom fee in woocommerce that is calculated after tax -
i've added custom fee checkout page following code:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' ); function woocommerce_custom_surcharge() { global $woocommerce; if ( is_admin() && ! defined( 'doing_ajax' ) ) return; $percentage = 0.03; $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $woocommerce->cart->tax_total ) * $percentage; $woocommerce->cart->add_fee( 'my fee', $surcharge, false, '' ); }
everything works expected except adding in tax. thought adding
$woocommerce->cart->tax_total
would trick, value returning 0.
is there way calculate tax before fee calculated?
i'm not sure if correct way handle issue.
but, i've got working solution problem.
i calculated tax , added fee calculation.
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' ); function woocommerce_custom_surcharge() { global $woocommerce; if ( is_admin() && ! defined( 'doing_ajax' ) ) return; $percentage = 0.03; $taxes = array_sum($woocommerce->cart->taxes); $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $taxes ) * $percentage; // make sure return false here. can't double tax people! $woocommerce->cart->add_fee( 'processing fee', $surcharge, false, '' ); }
i got following line woocommerce documentation. taking care set taxable variable false not double tax people.
$taxes = array_sum($woocommerce->cart->taxes);
Comments
Post a Comment