Change product price in cart programmatically
Here we will learn, how we can change product price in cart programmatically.
Add below code in your theme’s functions.php file
/**
* @snippet Change product price in cart programmatically
* @author Deep Prakash Goyal
*/
add_action( 'woocommerce_before_calculate_totals', 'wpexpertdeep_change_price_in_checkout' );
function wpexpertdeep_change_price_in_checkout( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
foreach ( $cart_object->get_cart() as $hash => $value ) {
$value[ 'data' ]->set_price( 10 );
}
}