Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

woocommerce_before_calculate_totals hook stopped working after update to WC 3.0.1

Tags:

woocommerce

I have updated to WC 3.0.1 from 2.6.14.
My original code is as follows:

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

It is no longer updating the price in cart or minicart.

like image 231
Gurvir Singh Avatar asked Apr 07 '17 18:04

Gurvir Singh


1 Answers

For overriding product price on cart in the latest version of Woocommerce (3.0.1) try using set_price( $price ) function in woocommerce this will help. Source here

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

    }

}
like image 189
Don't Stop Learning Avatar answered Nov 04 '22 20:11

Don't Stop Learning