Thanks for replying Subrata – the code on the site is:
add_action(‘wp_loaded’, function () {
if (isset($_GET[‘wallet-topup’])) {
$userId = get_current_user_id();
$cart = [];
foreach (WC()->cart->get_cart() as $item) {
array_push($cart, [
‘product’ => $item[‘product_id’],
‘quantity’ => $item[‘quantity’]
]);
}
update_user_meta($userId, ‘wallet_cart’, $cart);
$cartTotal = WC()->cart->get_cart_contents_total();
$quantity = round($cartTotal / 0.99);
WC()->cart->empty_cart();
$product = get_wallet_rechargeable_product();
WC()->cart->add_to_cart($product->get_id(), $quantity);
wp_redirect(wc_get_checkout_url());
exit;
}
if (isset($_GET['wallet-add-products-to-cart'])) {
$userId = get_current_user_id();
$cart = get_user_meta($userId, 'wallet_cart', true);
foreach ($cart as $item) {
WC()->cart->add_to_cart($item['product'], $item['quantity']);
}
wp_redirect(wc_get_checkout_url());
exit;
}
});
add_action(‘woocommerce_after_checkout_validation’, function ($fields, $errors) {
$product = get_wallet_rechargeable_product();
$topupWallet = false;
foreach (WC()->cart->get_cart() as $item) {
if ($item[‘product_id’] == $product->get_id()) {
$topupWallet = true;
}
}
if (!$topupWallet) {
$userId = get_current_user_id();
$cartTotal = WC()->cart->get_cart_contents_total();
$userBalance = woo_wallet()->wallet->get_wallet_balance($userId, ‘edit’);
if ($userBalance < $cartTotal) { $errors->add(‘validation’, ‘You do not currently have enough chips in your account, please top up your chips here‘);
}
}
}, 10, 2);
add_filter(‘woocommerce_add_cart_item_data’, function ($cartIitemData, $productId, $variationId, $quantity) {
$product = get_wallet_rechargeable_product();
if ($productId == $product->get_id()) {
$cartItemData[‘recharge_amount’] = $quantity;
}
return $cartItemData;
}, 10, 4);
add_filter(‘woocommerce_available_payment_gateways’, function ($gateways) {
if (is_admin()) {
return $gateways;
}
$product = get_wallet_rechargeable_product();
$topupWallet = false;
foreach (WC()->cart->get_cart() as $item) {
if ($item[‘product_id’] == $product->get_id()) {
$topupWallet = true;
}
}
if (!$topupWallet) {
unset($gateways[‘stripe’]);
}
return $gateways;
}, 10);
add_action(‘woocommerce_thankyou’, function ($orderId) {
$order = wc_get_order($orderId);
if ($order->get_payment_method() != ‘wallet’) {
?>
<?php
}
});