更新到WC 3.0.1后,woocommerce_before_calculate_totals钩子停止工作

Gur*_*ngh 5 woocommerce

我已经从2.6.14更新到WC 3.0.1.
我原来的代码如下:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $value['data']->price = $custom_price;
    }
}
Run Code Online (Sandbox Code Playgroud)

它不再更新购物车或迷你车的价格.

Don*_*ing 8

对于最新版本的Woocommerce(3.0.1)中的购物车价格,请尝试在woocommerce中使用set_price($ price)功能,这将有所帮助.来源于此

add_action( 'woocommerce_before_calculate_totals', 'woocommerce_pj_update_price', 99 );

function woocommerce_pj_update_price() {

    $custom_price = $_COOKIE["donation"]; // This will be your custom price  
    $target_product_id = 413; //Product ID

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        if($cart_item['data']->get_id() == $target_product_id){

            $cart_item['data']->set_price($custom_price);
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

  • 我浪费了 3 个小时的时间来寻找解决方案。多亏你不是3天... (3认同)