Help getting repeater entries from repeater field in PHP

By: Raimo Huybrechts | Asked: 06/21/2023
ForumsCategory: Code HelpHelp getting repeater entries from repeater field in PHP
Raimo Huybrechts asked 10 months ago

I've been trying to get the repeater entries from a repeater field in PHP but I can't manage to make it work. In the below code, $rooms should contain the list of values from the repeater field with field ID 22. Am I doing something wrong in my code when trying to retrieve that?

/**
 * Custom function to calculate the result for airco based on the given parameters.
 *
 * @param object $entry  The form entry object.
 *
 * @return float         The calculated result.
 */
function calculate_result_airco( $entry ) {
    // Get the value of the dropdown field for brand selection
    $brand_value = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => '20', 'entry' => $entry ) );
    echo "Brand Value: " . $brand_value . "<br>";
 
    // Get the value of the repeater field for rooms
    $rooms = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => '22', 'entry' => $entry ) );
    echo "Rooms: ";
    print_r($rooms);
    echo "<br>";
 
    // Calculate the total KW and price per room
    $total_kw = 0;
    $price_per_room = 0;
    $number_of_rooms = count($rooms);
    echo "Number of Rooms: " . $number_of_rooms . "<br>";
    
    foreach ($rooms as $room) {
        $surface_value = FrmProEntriesController::get_field_value_shortcode( array( 'field_id' => '25', 'entry' => $room['id'] ) );
        
        if ($surface_value >= 0 && $surface_value <= 15) {
            $kw = 2.5;
            $single_price = 376;
            $split_price = 968;
        } elseif ($surface_value > 15 && $surface_value <= 25) {
            $kw = 3.5;
            $single_price = 446;
            $split_price = 1123;
        } elseif ($surface_value > 25 && $surface_value <= 35) {
            $kw = 4.2;
            $single_price = 760;
            $split_price = 1785;
        } elseif ($surface_value > 35 && $surface_value <= 45) {
            $kw = 5;
            $single_price = 909;
            $split_price = 2283;
        } else {
            $kw = 7;
            $single_price = 1162;
            $split_price = 2328;
        }
        
        $total_kw += $kw;
        $price_per_room = ($number_of_rooms === 1) ? $split_price : $single_price;
    }
    echo "Total KW: " . $total_kw . "<br>";
    echo "Price Per Room: " . $price_per_room . "<br>";
 
    // Calculate the price of the exterior unit based on total KW and number of rooms
    $exterior_price = 0;
 
    if ($number_of_rooms === 2 && $total_kw >= 4) {
        $exterior_price = 1836;
    } elseif ($number_of_rooms === 2 && $total_kw >= 5.7) {
        $exterior_price = 1977;
    } elseif ($number_of_rooms === 3 && $total_kw >= 5.4) {
        $exterior_price = 2326;
    } elseif ($number_of_rooms === 3 && $total_kw >= 6.8) {
        $exterior_price = 2655;
    } elseif ($number_of_rooms === 4 && $total_kw >= 8) {
        $exterior_price = 3220;
    } elseif ($number_of_rooms >= 5 && $total_kw >= 9.5) {
        $exterior_price = 4228;
    }
    echo "Exterior Price: " . $exterior_price . "<br>";
 
    // Calculate the final result based on the brand selection and exterior price
    $result = 0;
 
    if ($brand_value === 'Daikin') {
        $result = ($price_per_room * $number_of_rooms + $exterior_price) * 1.15;
} elseif ($brand_value === 'Fujitsu') {
        $result = $price_per_room * $number_of_rooms + $exterior_price;
    }
    echo "Result: " . $result . "<br>";
 
    // Add a constant amount of €500 per interior unit to the final price
    $result += $number_of_rooms * 500;
    echo "Result with Interior Units: " . $result . "<br>";
 
    $result = apply_building_year_calculation( $entry, $result, '18' );
 
    return $result;
}
1 Answers
Rob LeVineRob LeVine answered 10 months ago
The repeater entries are in a separate "hidden" form. Its entries are based on the entry in the main form. Try code something like this. I have not tested it so you might need to adjust/correct.
Rob LeVineRob LeVine replied 10 months ago

While working on another project I realized that my solution only works if there's one repeating field in the form. If there are others, the SELECT command will return more than one value, which is not good.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right