Hi @rexyspy,
This can be done by some custom coding using WooCommerce woocommerce_add_to_cart_validation
filters.
Thread Starter
Rex AZ
(@rexyspy)
Thanks,
Am not good with coding, could you help with the code snippet to achieve this please?
I’d really appreciate it.
Thank you Subrata.
@rexyspy,
You can fetch user wallet balance by woo_wallet()->wallet->get_wallet_balance($user_id);
function where $user_id
is user id which you want to fetch balance.
Thank you.
Thread Starter
Rex AZ
(@rexyspy)
Hey Subrata,
You know I’ve been working on this code for sometime now, but no success. I have tried several codes and looked for ways around it, but to no avail. This was the closest I could get. This code seemed well to do the job, but am getting errors and my site was breaking…
/* Check woo-wallet balance upon clicking add to cart.
* If balance is insufficient display error message saying insufficient wallet balance,
* else proceed to cart or checkout.
*/
function check_balance_validation ($passed){
global &balance= woo_wallet()->wallet->get_wallet_balance($user_id);
if (is_user_logged_in()) && (&balance < wc_product_price) {
wc_add_notice (__('Your eWallet Balance is not sufficient to make the purchase', 'woocommerce'), 'error');
$passed = false;
}
return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'check_balance_validation');
Please, how can you help me get this done. I ‘ll be really grateful.
Regards.
Hi @rexyspy,
Please use below updated code and let me know.
/* Check woo-wallet balance upon clicking add to cart.
* If balance is insufficient display error message saying insufficient wallet balance,
* else proceed to cart or checkout.
*/
function check_balance_validation($passed, $product_id) {
$product = wc_get_product($product_id);
if (is_user_logged_in() && woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), 'edit') < $product->get_price('edit')) {
$passed = false;
wc_add_notice(__('Your eWallet Balance is not sufficient to make the purchase', 'woocommerce'), 'error');
}
return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'check_balance_validation', 10, 2);
Thread Starter
Rex AZ
(@rexyspy)
Subrata,
I am so grateful. It worked quite well as expected. I even added a bit to allow the user top up from the notice.
Thank you so much. Your plugin and support deserves a 10 star rating. That’s what I will do now.
Thanks for sharing this solution, 1+ grateful
This works for me too, tks for sharing
@subratamal
Good job man !
The above code isn’t working on my network site I’ve been trying to install the code via my snippets plugin as usual the way i do install codes to my functions.php
But yet the code was given invalid, instant help bro!
@subratamal I’m still waiting for your response.
@omo9jatoja
Please use any FTP client and use this code in active theme function.php file and let me know.
Thank you.
@subratamal thank you it later worked please can i make this work only on checkout page, i mean to allow the click on add to cart and then say if balance is insufficient on checkout instead of the disappearance of the tera wllet.
Asap response boss
I’m thinking of replacing this code
add_filter(‘woocommerce_add_to_cart_validation’, ‘check_balance_validation’, 10, 2);
With this
add_filter(‘woocommerce_checkout_validation’, ‘check_balance_validation’, 10, 2);
Hi all,
1. I used this code it works fine whenever I update product quantity on the cart page it does not show any error.
2. I am using 95% wallet limitation so I need to compare cart value with 95% balance amount how is this possible?
Please help for the same.
@subratamal i need a code that will automatically deduct the product amount from the wallet once added to the cart, cause i use add to cart redirect to checkout. So, i want my users to bypass the checkout page.
I’m thinking of replacing the below code
add_filter(‘woocommerce_add_to_cart_validation’, ‘check_balance_validation’, 10, 2);
With this
add_filter(‘woocommerce_checkout_validation’, ‘deduct_balance’, 10, 2);
????
-
This reply was modified 4 years, 9 months ago by
ZaacWilliam.