I added a WooCommerce custom fee depending on the cart value, and I try to get this fee on a custom quote page, but when I call the WC()->cart->get_fees()
function, it's always empty even if the total price include my custom fee.
This is how I added my custom fee :
add_action('woocommerce_cart_calculate_fees' , 'custom_discount');
function custom_discount( ){
if( WC()->cart->subtotal >= 150 ):
$discount = WC()->cart->subtotal * -0.03;
WC()->cart->add_fee( 'Remise au volume : 3%', $discount);
endif;
}
Everything is fine on my cart and checkout page, but I created a page-quote.php to print a quote, and even if the wc_cart_totals_order_total_html()
is giving me the price with my discount, the WC()->cart->get_fees()
function is always giving me an empty array...
Related issue not really helpful : Custom WooCommerce fee disappears at checkout
When I do a simple print_r( WC()->cart )
to see what's inside, I can see my fees in the [total] array. I don't know what's happening with those fees...
[totals:protected] => Array
(
[subtotal] => 6000.00
[subtotal_tax] => 0
[shipping_total] => 30.00
[shipping_tax] => 0
[shipping_taxes] => Array
(
)
[discount_total] => 0
[discount_tax] => 0
[cart_contents_total] => 6000.00
[cart_contents_tax] => 0
[cart_contents_taxes] => Array
(
)
[fee_total] => -900.00
[fee_tax] => 0
[fee_taxes] => Array
(
)
[total] => 5130.00
[total_tax] => 0
)
When I add the hook do_action('woocommerce_cart_calculate_fees')
right after the do_action('get_header')
of my page template, I can see fees in my WC()->cart->get_fees()
.
I don't think it's the best way to do this thing, but at least it works for now...