I was looking for a way to add a deposit on the TOTAL Cart Amount on a Woocommerce site (rather than just adding a deposit to each product line item).
I found the answer to this ingenious thread here: Deposit based on a percentage of total cart amount
Here's the code I ended up using:
add_action( 'woocommerce_cart_calculate_fees', 'booking_deposit_calculation' );
function booking_deposit_calculation( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -.50; // 50% off (negative)
// Get cart subtotal excluding taxes
$cart_subtotal = $cart_object->subtotal_ex_tax;
// or for subtotal including taxes use instead:
// $cart_subtotal = $cart_object->subtotal;
## ## CALCULATION ## ##
$calculated_amount = $cart_subtotal * $percent;
// Adding a negative fee to cart amount (excluding taxes)
$cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, false );
}
This creates a default of 50% deposit for every product on the Cart and Checkout page. Brilliant! (Using CSS, I was then able to style the descriptions on the front-end.)
However, I have a few products (one product category) on which I don't want to force this deposit.
So, here's my question:
How can I tweak the code to continue to enforce a default deposit but exclude the deposit from one product category (or the products in this category if I cannot exclude an entire category)?