Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change total and tax_total Woocommerce

I create custom checkout page with custom calculation. How i cant change total and tax_total via Ajax (or refresh page if need).

I create custom page for ajax request and set this code

$ss = new WC_Session_Handler();
$ss->set('tax_total',9999999);
$ss->save_data();
$ss->set('total',9999999);
$ss->save_data(); 

var_dump(WC());

On this page i can see my changes, but 'checkout page' nothing happens (even after refresh). How can I change the arbitrary total or tax_total.

like image 608
barseon Avatar asked Sep 30 '15 17:09

barseon


2 Answers

Try to use

add_action('woocommerce_calculate_totals', array($this, 'calculate_totals'), 10, 1);

function calculate_totals($totals){
//your code
}

Also it shoul be tax_total in cart object and you will can change it.

like image 188
DrHolera Avatar answered Oct 20 '22 03:10

DrHolera


I've had problems getting other solutions to work for me, but at least for v.3.0.1, this worked great:

add_action('woocommerce_cart_total', 'calculate_totals', 10, 1);

function calculate_totals($wc_price){
    $new_total = 0;
    foreach ( WC()->cart->cart_contents as $key => $value ) {
        //calculations here
    }

    return wc_price($new_total);
}
like image 31
ศรีธนญชัย Avatar answered Oct 20 '22 01:10

ศรีธนญชัย