Update Woocommerce product prices on cart/checkout pages

Share This Post

Update Woocommerce Product Prices on Cart/Checkout Pages

It’s been always seen that Woocommerce has only an option to tag the product price but if you want to update product price on cart/checkout pages, our provided snippets are going to help you to do this easily.

Note: WooCommerce Plugin must be configured in order to use these snippets.

Let’s check how it is working…

Snippet 1: Update product price on cart/checkout pages

This snippet can be used to update product price on cart/checkout page, it doesn’t matter whatever the price is set. See line#6 to replace the value.

				
					/**
  * Update product price on cart/checkout pages
  */
function ldninjas_filter_product_price( $cart ) {
    foreach ( $cart->get_cart() as $item ) {
        $item['data']->set_price( 100 );
    }
}
add_action( 'woocommerce_before_calculate_totals', 'ldninjas_filter_product_price' );
				
			

Snippet 2: Update variable product price on cart/checkout pages

The snippet will help to update variable product price on cart/checkout page. You just need to replace the price value in line#7.

				
					/**
  * Update variable product price on cart/checkout pages
  */
function ldninjas_filter_variable_product_price( $cart ) {
    foreach ( $cart->get_cart() as $item ) {
        if( wc_get_product( $item['product_id'] )->is_type( 'variable' ) ) {
            $item['data']->set_price( 100 );   
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'ldninjas_filter_variable_product_price' );
				
			

Snippet 3: Update Specific product price on cart/checkout pages

The snippet will help to update specific product price on cart/checkout page. Replace the price value in line#7.

				
					/**
  * Update specific product price on cart/checkout pages
  */
function ldninjas_filter_specific_product_price( $cart ) {
    foreach ( $cart->get_cart() as $item ) {
        if( $item['product_id'] == 12 ) {
            $item['data']->set_price( 100 );
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'ldninjas_filter_specific_product_price' );
				
			

Conclusion

So, All you need to add this code snippet to your theme functions.php file to update its product price on cart/checkout page on woocommerce sites. Enjoy!

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

How to get admin user ID

Share This Post How to get admin User ID? It’s been really difficult for many to get back the admin user ID but it’s so

Award GamiPress on RCPro Membership Renewal

Share This Post Award GamiPress on RCPro Membership Renewal Do you think users can also be awarded points/badges/ranks while RCPro Membership renewal? Yes, you can

Custom Message with myCRED Transfer

Share This Post Custom Message with myCRED Transfer MyCred assigns different points to the users but it’s been sometime noticed that the users want to

Want To Know About Our Services?

small_c_popup-300x296

Let’s Talk To Us With Your Concerns

    Once you send the details, we will be shortly back to you. Thanks!

    small_c_popup-300x296

    Become a valuable part of Team LDninjas

    small_c_popup-300x296

    Be updated with new release by LDninjas!