For displaying discounted price in discount table with least and highest price for variable product

Display discounted prices in a discount table, showing both the lowest and highest prices for variable products.

<?php
/**
 * List matched Rules in Table format
 *
 * This template can be overridden by copying it to yourtheme/plugin-folder-name/discount-table.php
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly
if (!isset($table_data) || empty($table_data)) return false;
$base_config = (is_string($data)) ? json_decode($data, true) : (is_array($data) ? $data : array());
$show_discount_title_table = isset($base_config['show_discount_title_table'])? $base_config['show_discount_title_table']: 'show';
$show_column_range_table = isset($base_config['show_column_range_table'])? $base_config['show_column_range_table']: 'show';
$show_column_discount_table = isset($base_config['show_column_discount_table'])? $base_config['show_column_discount_table']: 'show';
$product_price = $product->get_price();
if($product->get_type() == 'variable'){
    $child_prices     = array();
    $children = $product->get_children();
    foreach ( $children as $child_id ) {
        $child = FlycartWoocommerceProduct::wc_get_product($child_id);
        if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
            $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
        }
    }
    $maxProductId = 0;
    $minProductId = 0;
    if ( ! empty( $child_prices ) ) {
        $min_price = min( $child_prices );
        $max_price = max( $child_prices );
        if($min_price != $max_price){
            $maxProductIds = array_keys($child_prices, $max_price);
            $minProductIds = array_keys($child_prices, $min_price);
            if(isset($maxProductIds[0]))
                $maxProductId = $maxProductIds[0];
            if(isset($minProductIds[0]))
                $minProductId = $minProductIds[0];
        }
    }
    if($maxProductId){
        $maxProduct = FlycartWoocommerceProduct::wc_get_product($maxProductId);
        $max_product_price = $maxProduct->get_price();
    }
}
?>
<table class="woo_discount_rules_table">
    <thead>
    <tr class="wdr_tr_head">
        <?php if ($show_discount_title_table == 'show') { ?>
            <td class="wdr_td_head_title"><?php esc_html_e('Name', 'woo-discount-rules'); ?></td>
        <?php } ?>
        <?php if ($show_column_range_table == 'show') { ?>
            <td class="wdr_td_head_range"><?php esc_html_e('Range', 'woo-discount-rules'); ?></td>
        <?php } ?>
        <?php if ($show_column_discount_table == 'show') { ?>
            <td class="wdr_td_head_discount"><?php esc_html_e('Discount', 'woo-discount-rules'); ?></td>
        <?php } ?>
    </tr>
    </thead>
    <tbody>
    <?php
    $have_discount = false;
    $table = $table_data;
    foreach ($table as $index => $item) {
        if ($item) {
            foreach ($item as $id => $value) {
                ?>
                <tr class="wdr_tr_body">
                    <?php if ($show_discount_title_table == 'show') { ?>
                        <td class="wdr_td_body_title"><?php echo $table_data_content[$index.$id]['title']; ?></td>
                    <?php } ?>
                    <?php if ($show_column_range_table == 'show') { ?>
                        <td class="wdr_td_body_range"><?php echo $table_data_content[$index.$id]['condition']; ?></td>
                    <?php } ?>
                    <?php if ($show_column_discount_table == 'show') { ?>
                        <td class="wdr_td_body_discount">
                            <?php
                            if($value->discount_type == "percentage_discount"){
                                if($value->to_discount > 0){
                                    $discount = ($product_price / 100) * $value->to_discount;
                                    $discounted_price = $product_price - $discount;
                                    echo wc_price($discounted_price);
                                }
                            } else {
                                if($value->to_discount > 0){
                                    $discounted_price = $product_price - $value->to_discount;
                                    echo wc_price($discounted_price);
                                } else {
                                    echo $table_data_content[$index.$id]['discount'];
                                }
                            }
                            if($product->get_type() == 'variable'){
                                if($maxProductId){
                                    echo " - ";
                                    if($value->discount_type == "percentage_discount"){
                                        if($value->to_discount > 0){
                                            $discount = ($max_product_price / 100) * $value->to_discount;
                                            $discounted_price = $max_product_price - $discount;
                                            echo wc_price($discounted_price);
                                        }
                                    } else {
                                        if($value->to_discount > 0){
                                            $discounted_price = $max_product_price - $value->to_discount;
                                            echo wc_price($discounted_price);
                                        } else {
                                            echo $table_data_content[$index.$id]['discount'];
                                        }
                                    }
                                }
                            }
                            ?>
                        </td>
                    <?php } ?>
                </tr>
            <?php }
            $have_discount = true;
        }
    }
    if (!$have_discount) {
        ?>
        <tr class="wdr_tr_body_no_discount">
            <td colspan="2">
                <?php esc_html_e('No Active Discounts.', 'woo-discount-rules'); ?>
            </td>
        </tr>
        <?php
    }
    ?>
    </tbody>
</table>