Home / Attachments / Payment Plan Calculator
Duplicate Snippet

Embed Snippet on Your Site

Payment Plan Calculator

Ապառիկ արժեքների հաշվարկ և ցուցադրում

Code Preview
php
<?php
// Register a shortcode to display payment plans with minimalistic design
add_shortcode('pedro_payment_plans', 'pedro_display_payment_plans');
function pedro_display_payment_plans() {
    // Get the global product object
    global $product;
    // Ensure $product is valid
    if (!$product || !is_a($product, 'WC_Product')) {
        return ''; // Return nothing if $product is not valid
    }
    // Get the product price
    $product_price = $product->get_price();
    // Ensure product price is valid
    if (empty($product_price) || !is_numeric($product_price)) {
        return ''; // Return nothing if price is invalid
    }
    // Array of month plans
    $month_plans = [
        '12_month' => 12,
        '24_month' => 24,
        '36_month' => 36,
        '48_month' => 48,
        '60_month' => 60,
    ];
    // Start output buffering
    ob_start();
    echo '<div class="payment-plans">';
    echo '<h3>Ապառիկ</h3>';
    // Loop through each plan and calculate the price
    foreach ($month_plans as $field_name => $months) {
        $monthly_plan = calculate_plan($product_price, $months);
        // Display the calculated plan with minimalistic style
        echo '<div class="payment-plan ' . esc_attr($field_name) . '">';
        echo '<span class="plan-title">' . esc_html(sprintf('%d-Ամիս', $months)) . '</span>';
        echo '<span class="plan-price">' . esc_html(number_format($monthly_plan, 0, '.', ' ')) . ' ֏</span>';
        echo '</div>';
    }
    echo '</div>';
    // Return the output
    return ob_get_clean();
}
// Function to calculate the monthly plan price
function calculate_plan($product_price, $months) {
    // Calculate 1% of the product price
    $one_percent = $product_price * 0.01;
    // Calculate the monthly plan price
    return ($product_price / $months) + $one_percent;
}

Comments

Add a Comment