Reading list Switch to dark mode

    WooCommerce Set Custom Product Price When Adding To Cart

    Updated 10 December 2018

    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 –

    <?php
    
    /**
     * Plugin Name: Webkul Override Product Price on Add to Cart
     * Description: Add product to cart with override price
     * Author: Webkul
     * Author URI: https://webkul.com
     * Plugin URI: https://webkul.com
     */
    
    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 ?
    Find out 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 – http://hookr.io/actions/woocommerce_before_calculate_totals/

    That’s all for custom label for product on shop and single page. Thanks for your time 🙂

    . . .

    Leave a Comment

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


    7 comments

  • Maria Luz
    Hi.
    Thank you very much is what I was looking for to solve a problem in the current project I am working on
  • 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: https://www.stagingdomain.xyz/product/sap-tools/ 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)
      Hello,
      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'])){
      WC()->cart->add_to_cart($product);
      }
  • Peter
    That’s good solution for one produkt in cart.
    What about adding the same product few times with different prices?
  • Luigi van der Pal
    Why would you use a class to call just one function :). Seems like a bit of OOP overhead. Otherwise it’s fine.

    But can be simplified and do the same thing:

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

    add_action( ‘woocommerce_before_calculate_totals’, array( $this, ‘wk_update_price’ ) );

  • Antonio
    Hi Varun, good post. I’ve a question: could you fix the price of items to a same price if the cart amount is minimum €100?
    Example: I buy 4 items and the cart amount became €110…I need that by the 5th item the price is fixed to €10. Than tha cart amount with 5 items will be €120.
    I need this rule for a specific role.
    Do you think is possible?
    Thanks
  • MakeWebBetter
    Great information Varun. This will really help people. Glad to find this post.
  • Back to Top

    Message Sent!

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

    Back to Home