If wallet balance < than order total then redirect to Top Up

[ad_1]

Oh! I forgot to mention, at the moment I am using a temporary fix, so if the customer hasn´t got enough money in the wallet, he will be prompted with a wallet top up form on the last step of the checkout, as you can see here:

This is not optimal, as they will have to fill all the fields just to find out they have to do a top up…. but it may help somebody out there, so to achiveve this go to woocomerce/templates/checkout/payment.php and on line 32(at list in my file) you will find:

else {
				echo '<li class="woocommerce-notice woocommerce-notice--info woocommerce-info">' . apply_filters( 'woocommerce_no_available_payment_methods_message', WC()->customer->get_billing_country() ? esc_html__( 'Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements.', 'woocommerce' ) : esc_html__( 'Please fill in your details above to see available payment methods.', 'woocommerce' ) ) . '</li>'; // @codingStandardsIgnoreLine
			}

Well inside that else statement and under the echo just paste this:

<form method="post" action="">
			<div class="woo-wallet-add-amount">
				<?php
				$min_amount = woo_wallet()->settings_api->get_option( 'min_topup_amount', '_wallet_settings_general', 0 );
				$max_amount = woo_wallet()->settings_api->get_option( 'max_topup_amount', '_wallet_settings_general', '' );
				?>
				<p>
					<input type="number" style="max-width: 70%;" step="0.01" min="<?php echo esc_attr( $min_amount ); ?>" max="<?php echo esc_attr( $max_amount ); ?>" name="woo_wallet_balance_to_add" id="woo_wallet_balance_to_add" class="woo-wallet-balance-to-add input-text" placeholder="<?php esc_attr_e( 'Enter amount', 'woo-wallet' ); ?>" required="" />
					<input type="submit" class="button" value="<?php esc_attr_e( 'Add', 'woo-wallet' ); ?>" />
				</p>
				<?php wp_nonce_field( 'woo_wallet_topup', 'woo_wallet_topup' ); ?>
	</div>
</form>

Hi @poll0none You can use the attached code in the theme functions.php file to redirect the user to the wallet top-up page from the checkout page if there is not enough wallet balance.

add_action('template_redirect', 'redirect_if_no_wallet_balance');
if(!function_exists('redirect_if_no_wallet_balance')){
	function redirect_if_no_wallet_balance(){
		if(is_checkout()){
			if( wc()->cart->get_total('edit') > woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), 'edit') ){
				wp_safe_redirect( wc_get_endpoint_url( get_option( 'woocommerce_woo_wallet_endpoint', 'woo-wallet' ), 'add', wc_get_page_permalink( 'myaccount' ) ) );
			}
		}
	}
}

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer