Support » Plugin: TeraWallet - For WooCommerce » Force customer to use wallet if the balance if more than their order

  • Resolved dwdonline

    (@dwdonline)


    Currently, if the customer’s order total is less than what they have in their wallet, it forces them to use the amount and then use another payment module for the remainder. We would like to also force them to use their balance if the balance is more than their order total. So, for example, their total is $100, and they have $200 in their wallet, we would like to make them use their wallet to checkout and not another payment.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter dwdonline

    (@dwdonline)

    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');
    
    Plugin Author Subrata Mal

    (@subratamal)

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

    Thread Starter dwdonline

    (@dwdonline)

    @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?

    Plugin Author Subrata Mal

    (@subratamal)

    @dwdonline In that case you can use the above code. The code is correct.

    Thread Starter dwdonline

    (@dwdonline)

    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');
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.