I came up with the following code, but I still have an issue. It works, but if the customer selects a shipping method that costs more – and makes the order total more than the credit, it doesn’t work. I need to be able to make this code run again if the shipping amount is more.
if(!function_exists('woocommerce_available_payment_gateways_callback')){
function woocommerce_available_payment_gateways_callback($_available_gateways){
$customer_wallet = woo_wallet()->wallet->get_wallet_balance('', 'edit');
$customer_cart_total = WC()->cart->total;
if(!is_admin() && !is_wallet_rechargeable_cart() && ($customer_cart_total <= $customer_wallet)){
foreach ($_available_gateways as $id => $gateway){
if('wallet' != $id){
unset($_available_gateways[$id]);
}
}
}
return $_available_gateways;
}
}
add_filter('woocommerce_available_payment_gateways', 'woocommerce_available_payment_gateways_callback');
@dwdonline If the order total is more than the wallet balance then this code will not work and the partial payment option will be activated.
@subratamal – I have the option checked to force them to use the wallet if their balance is less. But, I want customers to have to use the wallet if they have more funds in the wallet than the order total. Is there a way to do this?
@dwdonline In that case you can use the above code. The code is correct.
Ok, after a bit more work, I’ve solved this and figured I would share it with others looking for the same solution. This code will hide other payment methods if their balance is higher than the available credit, and then show the payment methods again if their balance is lower than the cart total. I had to take into account the shipping, fees, taxes, and subtotal.
if(!function_exists('dwd_woo_wallet_woocommerce_available_payment_gateways')){
function dwd_woo_wallet_woocommerce_available_payment_gateways($availableGateways){
$customerWallet = woo_wallet()->wallet->get_wallet_balance('', 'edit');
$customerCartTotal = WC()->cart->subtotal;
$chosenMethods = WC()->session->get( 'chosen_shipping_methods' );
$chosenPaymentMethod = WC()->session->get( 'chosen_payment_method' );
$chosenShipping = $chosenMethods[0];
$shippingCost = number_format(WC()->cart->shipping_total, 2, '.', '');
$shippingCost = (float)$shippingCost;
$cartFees = WC()->cart->get_fees();
foreach ($cartFees as $cartFee => $feekKey) {
foreach ($feekKey as $feekKey => $feeData) {
if ($feekKey == 'total') {
if ($feeData > 0) {
$totalFee= $feeData;
}
}
}
}
$cartTaxes = WC()->cart->get_taxes_total();
if(!is_admin() && !is_wallet_rechargeable_cart() && (($customerCartTotal + $shippingCost + $totalFee + $cartTaxes) <= $customerWallet)){
foreach ($availableGateways as $id => $gateway){
if('wallet' != $id){
unset($availableGateways[$id]);
}
}
}
return $availableGateways;
}
}
add_filter('woocommerce_available_payment_gateways', 'dwd_woo_wallet_woocommerce_available_payment_gateways');