Hello
Thank you for your good plugin
I want the amount to be refunded to the customer’s wallet when I put an order in refunded status. I have the following code which works correctly and in the wallet meta key of that user in the database, balance is updated correctly. But the problem is in the backend on the user’s profile page nor on his wallet and on the frontend in the user’s profile, this balance is not updated
add_action('woocommerce_order_status_refunded', 'refund_to_wallet_on_order_refunded');function refund_to_wallet_on_order_refunded($order_id) {
// Initial log to check action call
error_log("Refund action triggered for Order ID: $order_id");
// Get order and user information
$order = wc_get_order($order_id);
$user_id = $order->get_user_id();
$total_refund = $order->get_total();
// Checking that the user and order amount exist
if ($user_id && $total_refund > 0) {
// Use the correct meta key for the wallet
$wallet_balance = get_user_meta($user_id, '_current_woo_wallet_balance', true);
// Convert empty value to zero if not initialized
$wallet_balance = !empty($wallet_balance) ? $wallet_balance : 0;
// Calculate the new balance of the wallet
$new_balance = $wallet_balance + $total_refund;
update_user_meta($user_id, '_current_woo_wallet_balance', $new_balance);
// Add a note to the order
$order->add_order_note(sprintf(
'Amount of %s was added to the customer's wallet due to the refund of the order.',
wc_price($total_refund)
));
// Log to check previous and new inventory values
error_log("Old Wallet Balance: $wallet_balance, New Wallet Balance: $new_balance");
} else {
// Error log to check for problem with user value or order amount
error_log("Refund failed: Invalid user ID or refund amount. User ID: $user_id, Refund Amount: $total_refund");
}
}
What is the problem? Please help me complete this task and complete this code if needed?
Best Regars