I'm not good in WP and plugins and will need some help here. I've read that is possible to alter some WC core function with filters and action but probably I didn't understand this correctly. I want to simply delete the shipping price from my cart page Total price field.

The function which is in woocommerce/includes/class-wc-cart-totals.php is:

protected function calculate_totals() {
    $this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + $this->get_total( 'shipping_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
    $this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );

    // Allow plugins to hook and alter totals before final total is calculated.
    if ( has_action( 'woocommerce_calculate_totals' ) ) {
        do_action( 'woocommerce_calculate_totals', $this->cart );
    }

    // Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
    $this->cart->set_total( max( 0, apply_filters( 'woocommerce_calculated_total', $this->get_total( 'total' ), $this->cart ) ) );
}

I've tried to copy it to my child theme function.php file like this:

add_action('calculate_totals', 'my_calculate_totals');

function my_calculate_totals() 
{
        $this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
        $this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );

        // Allow plugins to hook and alter totals before final total is calculated.
        if ( has_action( 'woocommerce_calculate_totals' ) ) {
            do_action( 'woocommerce_calculate_totals', $this->cart );
        }

        // Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
        $this->cart->set_total( max( 0, apply_filters( 'woocommerce_calculated_total', $this->get_total( 'total' ), $this->cart ) ) );
}

It doesn't work. Is this possible?

My goal is to just remove this from first line

$this->get_total( 'shipping_total', true )

UPDATE:

I have tried like this

add_action( 'woocommerce_calculate_totals', 'my_calculate_totals');

function my_calculate_totals($cart = null) 
{
        $cart = new WC_Cart_Totals();
        $this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
        $this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );

}

Got this error

A valid WC_Cart object is required

  • Any suggestion with what and how to replace $this? – Ivanov Dec 7 at 13:39
  • That calculate_totals() function is a method in the WC_Cart_Totals class and if you want to override that method, you need to extend that class. $this basically points to the class instance - e.g. $instance = new WC_Cart_Totals();, and from a global function, $this is not available - and as you've seen, throws a fatal error. – Sally CJ Dec 7 at 15:16
  • PS: Whomever you were replying to, he/she already deleted his/her comments; so you should as well.... – Sally CJ Dec 7 at 15:16
  • 1
    @SallyCJ this is how I've done it (I've used the snipped from other question you helped me). I'm not sure if is "most optimized" variant but at least is working: pastebin.com/0GLnR9Yx – Ivanov Dec 12 at 9:08
  • 1
    That's also how I'd do it, except you should use the $total that WooCommerce passes to the filter callback function, so the $total = $cart_item->get_shipping_total(); is not necessary. I'm not asking you to use this, but you can try it out. – Sally CJ Dec 12 at 9:32

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.