I'm trying to remove the shipping cost from the cart totals, which I have got working on the CART and the CHECKOUT page using the following code.
However, on the ORDER CONFIRMATION page the shipping is not being removed.
Working for Cart and Checkout
add_action( 'woocommerce_after_calculate_totals', 'woocommerce_after_calculate_totals', 110 );
function woocommerce_after_calculate_totals( $cart ) {
// make magic happen here...
// use $cart object to set or calculate anything.
## Get The shipping totals
$shipping = WC()->cart->get_shipping_total();
$totalincshipping = $cart->total;
$cart->total = $totalincshipping-$shipping;
}
I tried the following for the ORDER CONFIRMATION page, but this does not give the desired result:
add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 100, 2 );
function change_total_on_checking( $order ) {
// Get order total
$shipping = $order->get_shipping_total();
$total = $order->get_total();
## -- Make your checking and calculations -- ##
$new_total = $total - "10"; // <== Fake calculation
// Set the new calculated total
$order->set_total( $new_total );
}
Any advice?
You can just use the woocommerce_calculated_total
filter hook, which allow plugins to filter the grand total, and sum the cart totals in case of modifications.
// Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
function filter_woocommerce_calculated_total( $total, $cart ) {
// Get shipping total
$shipping_total = $cart->get_shipping_total();
return $total - $shipping_total;
}
add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 10, 2 );
No further adjustments or additional code is necessary, as the cart total will be saved/stored and automatically used on other pages
UPDATE
Since you are using the Bayna – Deposits & Partial Payments for WooCommerce plugin, this may conflict with my answer, as the plugin you are using contains the same filter hook
Namely in the \deposits-for-woocommerce\src\Cart.php file on line 15 version 1.2.1
add_filter( 'woocommerce_calculated_total', [$this, 'recalculate_price'], 100, 2 );
So to make my answer code run after this hook, change it's priority to 100+
Replace in my answer
add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 10, 2 );
With
add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 110, 2 );
Credits: Bossman