Reputation: 2329
I want to update my cart total in the checkout page by a certain percentage. Like in custom PHP, $cart = $cart_total - ($cart_total * 0.15);
I have wrote the following in functions.php
add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
function custom_cart_total($discount_percentage) {
// actual cart amount is = $195.00
// $discount_percentage = 0.15;
$actual = WC()->cart->total;
WC()->cart->total =+ (WC()->cart->total *= $discount_percentage);
WC()->cart->total =- WC()->cart->total + $actual;
echo $ordertotal = wp_kses_data( WC()->cart->get_total() ); //die();
// $ordertotal is showing the expected amount $165.75;
return WC()->cart->total += $ordertotal; // It shows the $195.00
exit();
}
The cart still shows $195.00 but it should be $165.75; How can I modify the functionality?
Upvotes: 1
Views: 11513
Reputation: 254383
You are not using the right hook… try the following instead:
add_filter( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 );
function custom_calculated_total( $total, $cart ){
return round( $total - ($total * 0.15), $cart->dp );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Upvotes: 3