My customers can pay for their order via store credit (saved as user data), partly or completely. In case the store credit is enough to cover for the whole order, the customer can only choose Store credit payment method and the order is passed as paid (and the customer's credit adjusted).
But for part payment, I subtract the store credit from the order's total so that the customer is only left with part of the order to pay via the payment method of their choice (here the store credit method becomes unavailable).
To achieve this, I alter the order's total using $order->set_total:
function update_customer_credit_and_display_payment_method($order) {
//Calculate new total
$order_total = (float) WC()->cart->total;
$customer_id = get_current_user_id();
if (!$customer_id ) { return; }
$customer_credit = get_deposit_credit($customer_id);
$applied_credit = 0;
if ($order_total > $customer_credit && $customer_credit > 0) {
$actual_payment_method = $order->get_payment_method_title();
$applied_credit = $customer_credit;
$payment_method = $actual_payment_method . ' & Deposit Credit (' . wc_price($applied_credit) . ')';
$order->set_payment_method_title($payment_method);
$order->set_total($order_total - $applied_credit);
$order->save();
}
}
add_action('woocommerce_checkout_create_order', 'update_customer_credit_and_display_payment_method', 10, 1);
The problem is that when I pass an order from On hold to Processing (in case someone pays by cheque or bank transfer), it reverts the order's total to its original value (before the deposit is applied). Also happens when I set the order to completed. Is this a bug or am I not using the right function? Is there a way to alter the order's total for good? (without altering the price of products and taxes). Thank you.
You can use the following alternative, to avoid this issue, replacing your code with:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.