Change product price in cart programmatically

  • August 24, 2022

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

Related Post


Change “in stock” / “out stock” text in Woocommerce single product page

In this tutorial you will learn how we can change “in stock” / “out stock” text with the custom. We will use custom snippet for this. Just copy the above snippet and paste in your […]


Change Woocommerce sold individually product setting Programmatically

Let’s assume you have a product inventory of 1000s of product and you want to sell your all product individually, what will you? Well you need to change the product setting one by one to […]


Remove Added to Cart Message in Woocommerce

If you are looking a way to hide “added to cart message” with easy method? Then you are in right place. To we will learn how to remove the “added to cart message” without using […]