#7923
Keraton
Participant

I would like to supplement the previously described issue with my perspective on a solution. As a developer, I see a way to resolve the problem as follows:

Solution:

We need to ensure that when calculating the amount to be deducted from the internal wallet, both the product prices and the added fees are taken into account. To achieve this, you can try one of the following approaches:

1. Using a Filter to Recalculate the Order Total
If TeraWallet provides a filter to override the total amount (for example, woo_wallet_order_total), you can add the following code:

   php
   /**
    * Filter the total amount for wallet deduction, including the fees.
    */
   add_filter( 'woo_wallet_order_total', 'keraton_include_fees_in_wallet_total', 10, 2 );
   function keraton_include_fees_in_wallet_total( $total, $order_id ) {
       $order = wc_get_order( $order_id );
       if ( ! $order ) {
           return $total;
       }
       
       // Calculate the total amount of fees
       $fees_total = 0;
       foreach ( $order->get_items( 'fee' ) as $fee_item ) {
            $fees_total += (float) $fee_item->get_total();
       }
       
       // Return the adjusted total (products total + fees)
       return $total + $fees_total;
   }

2. Adjusting the Balance via a Hook After Successful Payment
If the filter is not available, you can use order status change hooks to manually adjust the user’s balance by deducting the missing amount:

   php
   /**
    * After successful payment, adjust the wallet deduction to include the fee amount,
    * if the payment method is TeraWallet.
    */
   add_action( 'woocommerce_order_status_pending_to_processing', 'keraton_adjust_wallet_balance_for_fees', 10, 2 );
   add_action( 'woocommerce_order_status_pending_to_completed', 'keraton_adjust_wallet_balance_for_fees', 10, 2 );
   function keraton_adjust_wallet_balance_for_fees( $order_id, $order ) {
       if ( 'terawallet' !== $order->get_payment_method() ) {
           return;
       }
       
       $fees_total = 0;
       foreach ( $order->get_items( 'fee' ) as $fee_item ) {
            $fees_total += (float) $fee_item->get_total();
       }
       
       if ( $fees_total > 0 ) {
            // It is assumed that the function woo_wallet_subtract_balance exists and handles the wallet deduction.
            woo_wallet_subtract_balance( $order->get_user_id(), $fees_total, $order_id, 'Adjusting wallet deduction to include fees for order renewal' );
       }
   }

These modifications will ensure that the correct amount, including the fees, is deducted from the user’s internal wallet when renewing an order.

I hope this solution helps to resolve the issue. I look forward to your feedback and further cooperation.

  • This reply was modified 1 month ago by Keraton.
  • This reply was modified 1 month ago by Keraton.
WhatsApp