If wallet balance < than order total then redirect to Top Up
-
First of all my congratulations for an incredible plug-in.
I got the plugin working like a charm, the only gateway I will use is the wallet. So whenever a customer hits [Procceed to checkout] I want to calculate the cart total and if the total is greater than the amount the customer has on the wallet, he will be redirected to make a top up instead of proceeding to checkout.
Could you maybe throw some light at this? I am not bad at php, but I am a mess with woocommerce hooks.
Kind regards, Alex.
-
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: https://streamable.com/k0ccmc
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' ) ) ); } } } }
@subratamal You are #1
But I am now in an eternal loop ?? I will upload a video in a sec.
- This reply was modified 1 year, 8 months ago by poll0none.
Please have a look: https://streamable.com/qt7q0g
- This reply was modified 1 year, 8 months ago by poll0none.
@poll0none You can use the updated code.
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( add_query_arg(array('from_checkout' => 1), wc_get_endpoint_url( get_option( 'woocommerce_woo_wallet_endpoint', 'woo-wallet' ), 'add', wc_get_page_permalink( 'myaccount' ) ) ) ); } } } }
This code will add a get parament in the url from_checkout using that you can check if it is redirected from checkout page or not.
@subratamal I edited previous answers thinking it would take you way longer to answer.
This is a fantastic way to detect if customer is redirected from checkout and display message if so.
The problem is that the code above just goes into an infinite loop as shown on this video: https://streamable.com/qt7q0g
@poll0none Sorry about this. Please use the attached updated code.
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() && !is_wallet_rechargeable_cart()){ if( wc()->cart->get_total('edit') > woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), 'edit') ){ wp_safe_redirect( add_query_arg(array('from_checkout' => 1), wc_get_endpoint_url( get_option( 'woocommerce_woo_wallet_endpoint', 'woo-wallet' ), 'add', wc_get_page_permalink( 'myaccount' ) ) ) ); } } } }
@subratamal Oh god, I could cry!
This is absolutely amazing, thank you for your time and help, its much appreciated.
If anybody else is planning on using your plugin in a similar way, the code to detect if the client is redirected to Top Up from checkout because of insufficient funds is as follows:
if (strpos($_SERVER['REQUEST_URI'], "from_checkout") !== false){ // Client has been redirected from the checkout because of insufficient funds. $walletTotal = woo_wallet()->wallet->get_wallet_balance( get_current_user_id() ); $cartTotal = WC()->cart->get_cart_subtotal(); }
I am thinking of buying the referral add-on, but plesase let me know if I can contribute to your plugin in any other way. You are fantastic and you deserver to see your plugin skyrocket.
Looking forward to hearing from you.
@poll0none as contribution one review will boost our team to work more hard for this plugin also you can donate to our plugin from the donation button in the plugin page. We alway try to response to our plugin support topic but in some case we unable to do because of handling other things like plugin update or fixing issues.
@subratamal Just left you a 5 star review and I was going to donate, but I am completely against paypal. If you share a credir card option for a donation I will be happy to donate.
Thank you so much for taking the time to leave us a 5-star review! We’re thrilled to hear that you’re enjoying our WordPress plugin and that it’s meeting your needs. Reviews like yours help us improve our plugin and make it even better for everyone.
Your positive feedback means a lot to our team, and we appreciate your support. It’s reviews like yours that keep us motivated and remind us why we do what we do.
We’re constantly working to make our plugin even more useful and user-friendly, and we hope that you’ll continue to find it helpful in the future. If you have any suggestions or feedback for us, please don’t hesitate to let us know.
Here is the donation link https://donate.stripe.com/fZeaFydax6NNfjWeVc
Thank you again for your kind words and for being a part of our community!
Dear@poll0none and @subratamal,
Please, where do I insert the code below for it to take effect on my website?
if (strpos($_SERVER['REQUEST_URI'], "from_checkout") !== false){ // Client has been redirected from the checkout because of insufficient funds. $walletTotal = woo_wallet()->wallet->get_wallet_balance( get_current_user_id() ); $cartTotal = WC()->cart->get_cart_subtotal(); }
I’ve added it to my child theme function, but it is throwing an error.
Please advise.
@ndiebere You don’t need to use this code. This code is for adding amount field in the wallet topup page.
- The topic ‘If wallet balance < than order total then redirect to Top Up’ is closed to new replies.