WooCommerce: Force Cart to Specific Amount (Deposit)

Here’s a very simple snippet that achieves a very complex task – what if you wanted to force your Cart to charge a deposit or a fixed fee, no matter the cart total?

Well, thankfully WooCommerce is pretty flexible and a lot of workarounds can be found.

In this case, we will study two possible solutions: (1) a negative “cart fee” to make the total become e.g. $100 and (2) a filter to completely override the calculated cart total e.g. $100.

Sounds like Japanese? Great – here’s why you’re on Business Bloomer. Copy the snippet, apply it to your test WooCommerce site and see the magic happen – without knowing anything about coding!

WooCommerce: force cart total to $100 no matter what is added to cart

PHP Snippet 1: Force Cart to Specific Amount (Deposit) Via a Negative Fee – WooCommerce

/**
 * @snippet       WooCommerce Deposit (Negative Fee)
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

// Note: this will force Cart to $100

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_woocommerce_deposit' );

function bbloomer_woocommerce_deposit() {
    $total_minus_100 = WC()->cart->get_cart_contents_total() - 100;
    WC()->cart->add_fee( 'Balance', $total_minus_100 * -1 );
}

PHP Snippet 2: Force Cart to Specific Amount (Deposit) Via a Filter – WooCommerce

/**
 * @snippet       WooCommerce Deposit (Filter)
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

// Note: this will force Cart to $100

add_filter( 'woocommerce_calculated_total', 'bbloomer_woocommerce_deposit_filter', 9999, 2 );

function bbloomer_woocommerce_deposit_filter( $total, $cart ) {
	return 100;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

13 thoughts on “WooCommerce: Force Cart to Specific Amount (Deposit)

  1. Hello Rodolfo,
    Thank you for your code! I wonder how to add functionality so that the user would not be able to checkout if the balance is negative.

    Best,
    Jin

  2. I made a slight change to the code to suit my purposes. Its not perfect but it works for me for the moment.

      WC()->cart->add_fee( "The balance of ยฃ" . $total_minus_150 . " in full is required 4 weeks before Hire Date", $total_minus_150 * -1 );
  3. Hey Rodolfo,
    I have just used the code today 20/10/2021 to Force Cart to specific amount. It works ok. I need to figure out the minus display issue thought lol.

    WordPress 5.81
    PHP 7.3.3
    Apache

  4. can we add the custom deposit price
    in single product

    1. Hi Sarfaraz, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. Hi,

    The fee shows a minus figure (naturally). How would you have it show a positive figure to the end user and calculates as a negative. So for example, if you were using ยฃ5 deposit but wanted the fee to show the total outstanding minus the ยฃ5, how would you do this so it shows up on all places including emails.

    This is what i’m after:

    Total ………………………… ยฃ 30
    Pay on collection ………. ยฃ25
    Deposit …………………….. ยฃ 5

    This is what the code gives me:

    Total ………………………… ยฃ 30
    Pay on collection ………. -ยฃ25
    Deposit …………………….. ยฃ 5

    1. Hiya Daniel, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  6. Hi Rodolf, I am looking for something similar to this, I will check this code, thank you for sharing.

    1. Nice ๐Ÿ™‚

  7. I have a lodge which charges a deposit online (with PayPal) and customers can pay the rest when they arrive in cash to save on fees etc.
    I want to charge a 20% deposit amount of the cart total at checkout, without any extra outstanding costs they have to pay. I’ve looked at the deposit plugin solutions but they have more options than I need.

    Is it possible to modify this code to calculate 20% of cart total and only charge this at checkout?

    I can then send an e-mail with the amount outstanding separately.

    Thanks for your advice!

    Ben

    1. Ben, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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