Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    woocommerce-hyperlocal-marketplace-guide

    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 –


    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.

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

    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 🙂

    . . .

    Comments (3)

    Add Your Comment

  • 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
      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?
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close