Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter mamagee

    (@mamagee)

    This the updated version of the code not tried about to

    <?php
    
    // Hook to initiate interest calculation after order completion
    add_action('woocommerce_order_status_completed', 'start_interest_calculation');
    
    function start_interest_calculation($order_id) {
        $completed_time = get_post_meta($order_id, '_completed_date', true);
        $current_time = current_time('timestamp');
        $elapsed_time = $current_time - strtotime($completed_time);
    
        if ($elapsed_time < 86400) { // 86400 seconds = 24 hours
            $order_amount = get_post_meta($order_id, '_order_total', true);
            $interest = $order_amount * 0.25;
    
            // Code to add interest amount to user's WooCommerce wallet
            update_user_wallet($order_id, $interest);
    
            // Schedule event to stop interest calculation after 24 hours
            wp_schedule_single_event(current_time('timestamp') + 86400, 'stop_interest_calculation', array($order_id));
        }
    }
    
    // Hook to stop interest calculation after 24 hours
    add_action('stop_interest_calculation', 'stop_interest_calculation');
    
    function stop_interest_calculation($order_id) {
        // Code to stop interest calculation for the given order
        update_order_interest_status($order_id);
    }
    
    // Hook to restart interest calculation and refund old order on new order placement
    add_action('woocommerce_new_order', 'restart_interest_calculation');
    
    function restart_interest_calculation($order_id) {
        $order = wc_get_order($order_id);
        if (!$order) return;
    
        $user_id = $order->get_user_id();
        $cycle_start_date = get_cycle_start_date($user_id);
        $cycle_day = (current_time('timestamp') - strtotime($cycle_start_date)) / DAY_IN_SECONDS;
    
        if ($cycle_day < 15 && $order->get_status() === 'completed') {
            refund_old_order($order_id);
            start_interest_calculation($order_id);
        } else {
            set_cycle_start_date($user_id); // Reset the cycle
        }
    }
    
    // Hook to handle the 15th day special case
    add_action('woocommerce_new_order', 'handle_15th_day_special_case');
    
    function handle_15th_day_special_case($order_id) {
        $user_id = get_post_meta($order_id, '_customer_user', true);
        if (!$user_id) return;
    
        $cycle_start_date = get_cycle_start_date($user_id);
        $cycle_day = (current_time('timestamp') - strtotime($cycle_start_date)) / DAY_IN_SECONDS;
    
        if ($cycle_day == 15) {
            return; // Do not calculate interest or refund on the 15th cycle day
        }
    
        start_interest_calculation($order_id);
    }
    
    // Functions - Implement these based on your requirements
    
    /**
     * Function to update user's WooCommerce wallet with interest amount.
     *
     * @param int $order_id Order ID.
     * @param float $interest Interest amount to be added to the wallet.
     */
    function update_user_wallet($order_id, $interest) {
        // Get the user ID associated with the order
        $user_id = get_post_meta($order_id, '_customer_user', true);
    
        // Check if a user ID is available
        if ($user_id) {
            // Get the current wallet balance from user meta
            $current_wallet_balance = get_user_meta($user_id, 'wallet_balance', true);
    
            // If the user meta doesn't exist, initialize the wallet balance
            if (!$current_wallet_balance) {
                $current_wallet_balance = 0.0;
            }
    
            // Calculate the new wallet balance after adding interest
            $new_wallet_balance = $current_wallet_balance + $interest;
    
            // Update user meta with the new wallet balance
            update_user_meta($user_id, 'wallet_balance', $new_wallet_balance);
        }
    }
    
    /**
     * Function to update order interest status.
     *
     * @param int $order_id Order ID.
     */
    function update_order_interest_status($order_id) {
        // Implement code to update order interest status
        // Example: update_post_meta($order_id, '_interest_calculated', true);
    
        // Adding custom meta field '_interest_calculated' to mark interest as calculated
        update_post_meta($order_id, '_interest_calculated', true);
    }
    
    /**
     * Function to refund old order amount along with interest to user's WooCommerce wallet.
     *
     * @param int $order_id Order ID.
     */
    function refund_old_order($order_id) {
        // Get the order object
        $order = wc_get_order($order_id);
    
        // Ensure the order exists
        if ($order) {
            // Calculate the refund amount (order total + interest)
            $refund_amount = $order->get_total() + get_post_meta($order_id, '_order_interest', true);
    
            // Create a refund for the entire order amount
            $refund = wc_create_refund(array(
                'order_id'     => $order_id,
                'amount'       => $refund_amount,
                'reason'       => 'Interest Refund',
                'refund_id'    => uniqid('refund_'), // Generate a unique refund ID
            ));
    
            if (is_wp_error($refund)) {
                // Handle error
                error_log('Error creating refund: ' . $refund->get_error_message());
            } else {
                // Update order meta to track that a refund has been processed
                update_post_meta($order_id, '_refund_processed', true);
    
                // Optionally update other order-related information
                // ...
    
                // Log refund information
                error_log('Refund created successfully for order ID: ' . $order_id);
            }
        }
    }
    
    // Add function to set the cycle start date
    function set_cycle_start_date($user_id) {
        update_user_meta($user_id, 'cycle_start_date', current_time('Y-m-d'));
    }
    
    // Add function to get the cycle start date
    function get_cycle_start_date($user_id) {
        return get_user_meta($user_id, 'cycle_start_date', true);
    }
    
    ?>
    
Viewing 1 replies (of 1 total)