$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Override woocommerce wc-class function|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Override woocommerce wc-class function

matteradmin11PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far