Back to Top

WooCommerce Set Custom Product Price When Adding To Cart

Updated 17 April 2024

WooCommerce Set Custom Product Price When Adding To Cart – In this post, we’ll see how we can override the price of product when adding the product into cart.

If you require expert assistance or want to develop custom unique functionality, hire WooCommerce Developers for your project.

Price overriding in cart is useful feature if we want to increase or decrease product price by any static value. This can be done by creating price rules also instead of static price value. This is very useful code snippet from developer point of view.

We’ll understand the code by taking an example with static price value. We’ll use action hook ‘woocommerce_before_calculate_totals’ to achieve our goal.

Let’s start with an example plugin –

Searching for an experienced
Woocommerce Company ?
Find out More

 * Plugin Name: Webkul Override Product Price on Add to Cart
 * Description: Add product to cart with override price
 * Author: Webkul
 * Author URI:
 * Plugin URI:

if ( ! class_exists( 'WK_Price_Override' ) ) {
  class WK_Price_Override {

    function __construct() {
      add_action( 'woocommerce_before_calculate_totals', array( $this, 'wk_update_price' ) );

    function wk_update_price( $cart_object ) {
      $cart_items = $cart_object->cart_contents;

      if ( ! empty( $cart_items ) ) {
        $price = 100;
	    foreach ( $cart_items as $key => $value ) {
          $value['data']->set_price( $price );

  function wk_initialise_main_class() {
    new WK_Price_Override();

  add_action( 'plugins_loaded', 'wk_initialise_main_class' );


In above code snippet, we have initialised main class with ‘plugins_loaded’ action hook. As mentioned earlier, we have used hook ‘woocommerce_before_calculate_totals’ and in callback function, we retrieved cart items from cart object argument.  And then we iterate through cart items and set price for each item. In this way we can override product price in cart.

Please check in below screenshots the difference between product price on product page and cart page.

WooCommerce Set Custom Product Price When Adding To Cart

As we can see product price is 18 USD. But from code we have override same to 100 USD. Check below the cart page –

WooCommerce Set Custom Product Price When Adding To Cart

Reference –

That’s all for custom labels for products on the shop and single page. 


For any technical assistance kindly raise a ticket or reach us by email at [email protected]. Thanks for Your Time! Have a Good Day!

Also, discover various solutions to add more features and enhance your online store by visiting the WooCommerce plugins.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • Maria Luz
  • Matt
    • Amit Chauhan (Moderator)
  • Peter
  • Luigi van der Pal
  • Antonio
  • MakeWebBetter
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content