• Resolved digidoda

    (@digidoda)


    Hi there,

    On my site, I have it set so that users can only make purchases through their digital wallet, but when they don’t have enough in their wallet and they are re-directed to add funds, it is rounding up the needed funds to the nearest whole number – for example, the customer has £9.90 in their basket, but when they try and add funds to their wallet, it is rounding that up to £10.00 – is there any way of changing this so it represents that actual value of the products in their basket?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Subrata Mal

    (@subratamal)

    @digidoda Please share the code you are using for redirecting the user to add funds.

    Thread Starter digidoda

    (@digidoda)

    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
    }
    });

    Plugin Author Subrata Mal

    (@subratamal)

    @digidoda Please try attached updated code.

    add_action(
    	'wp_loaded',
    	function () {
    		if ( isset( $_GET['wallet-topup'] ) ) {
    			$userId = get_current_user_id();
    			$cart   = array();
    			foreach ( WC()->cart->get_cart() as $item ) {
    				array_push(
    					$cart,
    					array(
    						'product'  => $item['product_id'],
    						'quantity' => $item['quantity'],
    					)
    				);
    			}
    			update_user_meta( $userId, 'wallet_cart', $cart );
    			$cartTotal = WC()->cart->get_cart_contents_total();
    			if ( $cartTotal > woo_wallet()->wallet->get_wallet_balance( $userId, 'edit' ) ) {
    				$quantity = ceil( $cartTotal - woo_wallet()->wallet->get_wallet_balance( $userId, 'edit' ) );
    				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;
    		}
    	}
    );
    Thread Starter digidoda

    (@digidoda)

    Thanks Subrata – do I need to remove the rest of the code that I provided too or is this just replacing the first 3 sections of the code I provided?

    I have tried replacing the first three sections and the wallet topup is still rounding up to £25 when it should actually be £24.75

    Thanks, Mike.

    Thread Starter digidoda

    (@digidoda)

    I have also now tried it with just those 3 sections of code added and on the checkout page it is still showing as the whole number – is there a way of setting the digital wallet topup increment values to be 0.99 rather than 1 instead?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Tera Wallet Rounding Up Amount To Whole Number’ is closed to new replies.