Php 使用Woocommerce 3.2+中的挂钩更改购物车总数;

Php 使用Woocommerce 3.2+中的挂钩更改购物车总数;,php,wordpress,woocommerce,cart,hook-woocommerce,Php,Wordpress,Woocommerce,Cart,Hook Woocommerce,我想在woocommerce结帐页面的订单总数中添加300,但woocommerce\u calculate\u totals hook无法完成此任务 如果我使用var_dump($total),我会看到正确的结果-int(number),但订单表中的总金额没有改变 add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 ); function action_cart_calculate

我想在woocommerce结帐页面的订单总数中添加300,但woocommerce\u calculate\u totals hook无法完成此任务

如果我使用var_dump($total),我会看到正确的结果-int(number),但订单表中的总金额没有改变

add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );

function action_cart_calculate_totals( $cart_object) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( !WC()->cart->is_empty() ):


        $total = $cart_object->cart_contents_total += 300;

        var_dump($total);

    endif;
}
自从Woocommerce 3.2以来,hook
Woocommerce\u calculate\u totals
就不适用于此
请参阅此线程的说明:

您必须使用以下方法之一:

1) 过滤器挂钩
woocommerce\u按以下方式计算\u总数

add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
function change_calculated_total( $total, $cart ) {
    return $total + 300;
}
2) 收费标准如下:

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 );
}
代码位于活动子主题(或活动主题)的function.php文件或任何插件文件中

自从Woocommerce 3.2以来,hook
Woocommerce\u calculate\u totals
就不适用于此
请参阅此线程的说明:

您必须使用以下方法之一:

1) 过滤器挂钩
woocommerce\u按以下方式计算\u总数

add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
function change_calculated_total( $total, $cart ) {
    return $total + 300;
}
2) 收费标准如下:

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 );
}

代码会出现在活动子主题(或活动主题)的function.php文件中或任何插件文件中。

谢谢。很好用!一个问题:为什么在函数“change\u computed\u total”中使用第二个参数$cart。@VitKashchuk我在这里不使用第二个参数,我只是按它应该的方式设置它:记住答案对其他人有用。现在这个答案似乎回答了你的问题,你可以请你回答,谢谢。仍然很有用@VitKashchuk@LoicTheAztec如何将参数传递给此
更改\u计算的\u总计
。我想传递来自购物车页面的用户输入值。谢谢。很好用!一个问题:为什么在函数“change\u computed\u total”中使用第二个参数$cart。@VitKashchuk我在这里不使用第二个参数,我只是按它应该的方式设置它:记住答案对其他人有用。现在这个答案似乎回答了你的问题,你可以请你回答,谢谢。仍然很有用@VitKashchuk@LoicTheAztec如何将参数传递给此
更改\u计算的\u总计
。我想传递来自购物车页面的用户输入值。