What I'm trying to achieve is to charge customers a deposit based on the items they add to the cart. Only specific items require a deposit. For each of these items 1x deposit is added. As an example: Item1, Item2 & Item3 all require a deposit of 10.00 while Item4, Item5 & Item6 don't require a deposit. So if I had the following in the Cart
Product Qty Unit Price Subtotal
Item1 x1 £50.00 £50.00
Item2 x2 £30.00 £60.00
Item4 x1 £5.00 £5.00
Item6 x1 £2.99 £2.99
---------------------------------------------
Total: £117.99
Deposit: £30.00
Balance: £87.99
I would need to charge a deposit of £30 as there are 3 deposit items. This then leaves a remaining balance which the customer pays on collection of the items.
I've tried various plugins (WooCommerce Deposits, Yith Deposits/Part Payments) and none of them come close to achieving what I need.
I've got somewhere by adding Hooks into my child theme's functions.php file but I'm at a loss how to get it fully functional.
Current Code
add_action( 'woocommerce_cart_calculate_fees', 'tcg_calculate_deposit' );
function tcg_calculate_deposit( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Check if these products are in the cart
$deposit_items = array( '4359', '4336', '4331', '4320' );
$deposit = 10.00;
$matching = false;
foreach ( $cart_object->get_cart() as $item_values ) {
if( in_array( $item_values['product_id'], $deposit_items )) {
// for each item in array deposit + 1
// so if 3 deposit items deposit = 30.00
// set cart total to be the deposit amount so I can process this amount as payment through woocommerce
$cart_subtotal = $cart_object->subtotal_ex_tax;
$balance = $cart_subtotal - $deposit;
$cart_object->add_fee( __( 'Balance', 'woocommerce'), $balance, true );
break;
}
}
}
At the minute this makes the Total the sum of the Subtotal + Balance. I need it to be Deposit. I'm not sure if I need to create separate functions to achieve?