Posted on

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 product price?

Well, thankfully WooCommerce is pretty flexible and a lot of workarounds can be found. In this case, forcing the checkout to a fixed amount (e.g. $100) is as simple as applying a negative “cart fee” to make the total become $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 amount

PHP Snippet: Force Cart to Specific Amount (Deposit) – WooCommerce

 

/**
* @snippet Force Cart to Specific Amount (Deposit) - WooCommerce
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=72625
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.5.1
*/

// Note: this will force Cart to $100

add_action( 'woocommerce_cart_calculate_fees','bbloomer_force_cart_to_100' );

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

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

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

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

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

    1. Nice 🙂

  3. 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? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.