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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With