Read More
Menu Close

    WooCommerce Set Custom Product Price When Adding To Cart

    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.

    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 –

     * 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.

    Searching for an experienced
    Woocommerce Company ?
    Read More

    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 label for product on shop and single page. Thanks for your time 🙂

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


  • Matt
    Is there a way to add this to only occur when using a specific add to cart button? I added a second add to cart button here: and want it to act as a quote button (product price is $0) but also have the option for adding to cart with the normal price too.
    • Amit Chauhan (Moderator)
      yes that is equally possible as i have checked the link given by you,
      quote button can be placed with add to cart button. button that button must be inside the form 
      so that on submit you can check for the particular button submit and modify product price and can add it to cart as well
      like on submit what you can do is if(!empty($_POST['submitted_button'])){
  • Peter
    That’s good solution for one produkt in cart.
    What about adding the same product few times with different prices?
  • Back to Top