Change Cart total using Hooks in Woocommerce 3.2+

对着背影说爱祢 提交于 2019-12-06 14:35:36

Since Woocommerce 3.2, the hook woocommerce_calculate_totals doesn't work for that.
See explanations on this thread: Change Cart total price in WooCommerce

You will have to use one of the following ways using:

1) The filter hook woocommerce_calculated_total this way:

add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
function change_calculated_total( $total, $cart ) {
    return $total + 300;
}

2) The Fee API like:

add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee', 10, 1 );
function add_custom_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $fee = 300;

    $cart->add_fee( __( 'Fee', 'woocommerce' ) , $fee, false );
}

Code goes in function.php file of your active child theme (or active theme) or also in any plugin file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!