'How can I render the price calculation on WooCommerce?

I'm still a novice in WC, I've built an extra option to select for how many days you would like to book the service, in addition i need to make a calculation on different price, for example: if the customer will select 2x products it will cost €10 ( €5 per product ) for 2 days will be 20 as need to double it for each day,

I've built a function like that, but the frontend do not show an update price at the check out :

function booking_variations() {
    echo '<table class="booking-variations" cellspacing="0">
            <tbody>
                <tr>
                    <td class="label"><label for="num_giorni" name="option_num_days">Per quanti giorni?</label></td>
                    <td class="value">
                        <select name="option_num_giorni"/> 
                            <option name="option_num_day_one" value="1">1</option>
                            <option name="option_num_day_two" value="2">2</option>
                            <option name="option_num_day_three" value="3">3</option>
                            <option name="option_num_day_four" value="4">4</option>
                            <option name="option_num_day_five" value="5">5</option>
                            <option name="option_num_day_six" value="6">6</option>
                            <option name="option_num_day_seven" value="7">7</option>
                            <option name="option_num_day_eight" value="8">8</option>
                            <option name="option_num_day_nine" value="9">9</option>
                            <option name="option_num_day_ten" value="10">10</option>
                        </select>
                    </td>
                </tr>                             
            </tbody>
        </table>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'booking_variations' );

function save_booking_variations( $cart_item_data, $product_id ){
    if( isset( $_POST['option_num_day_one'] ) &&
    $_POST['option_num_day_one'] === '1' ) {
        $cart_item_data[ "add_num_fee_one" ] = "1";
    }
    
    if( isset( $_POST['option_num_day_two'] ) &&
    $_POST['option_num_day_two'] === '2' ) {
        $cart_item_data[ "add_num_fee_two" ] = "2";
    }
    
    if( isset( $_POST['option_num_day_three'] ) &&
    $_POST['option_num_day_three'] === '3' ) {
        $cart_item_data[ "add_num_fee_three" ] = "3";
    }
    
    if( isset( $_POST['option_num_day_four'] ) &&
    $_POST['option_num_day_four'] === '4' ) {
        $cart_item_data[ "add_num_fee_four" ] = "4";
    }
    
    if( isset( $_POST['option_num_day_five'] ) &&
    $_POST['option_num_day_five'] === '5' ) {
        $cart_item_data[ "add_num_fee_five" ] = "5";
    }
    
    if( isset( $_POST['option_num_day_six'] ) &&
    $_POST['option_num_day_six'] === '6' ) {
        $cart_item_data[ "add_num_fee_six" ] = "6";
    }
    
    if( isset( $_POST['option_num_day_seven'] ) &&
    $_POST['option_num_day_seven'] === '7' ) {
        $cart_item_data[ "add_num_fee_seven" ] = "7";
    }
    
    if( isset( $_POST['option_num_day_eight'] ) &&
    $_POST['option_num_day_eigh'] === '8' ) {
        $cart_item_data[ "add_num_fee_eight" ] = "8";
    }
    
    if( isset( $_POST['option_num_day_nine'] ) &&
    $_POST['option_num_day_nine'] === '9' ) {
        $cart_item_data[ "add_num_fee_nine" ] = "9";
    }
    
    if( isset( $_POST['option_num_day_ten'] ) &&
    $_POST['option_num_day_ten'] === '10' ) {
        $cart_item_data[ "add_num_fee_ten" ] = "10";
    }
    
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations', 99, 2 );

function calculate_num_days_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_one = 0;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_one"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_one);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_one);
                }
            }
        }
    }

    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_two = 10;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_two"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_two);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_two);
                }
            }
        }
    }

    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_three = 15;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_three"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_three);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_three);
                }
            }
        }
    }

    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_four = 20;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_four"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_four);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_four);
                }
            }
        }
    }

    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_five = 25;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_five"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_five);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_five);
                }
            }
        }
    }
    
    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_six = 30;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_six"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_six);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_six);
                }
            }
        }
    }

    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_seven = 35;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_seven"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_seven);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_seven);
                }
            }
        }
    }

    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_eight = 40;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_eight"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_eight);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_eight);
                }
            }
        }
    }

    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_nine = 45;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_nine"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_nine);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_nine);
                }
            }
        }
    }

    if( !WC()->session->__isset( "reload_checkout" )) {
        $add_price_ten = 50;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["add_num_fee_ten"] ) ) {
                if( method_exists( $value['data'], "set_price" ) ) {
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $add_price_ten);
                } else {
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice + $add_price_ten);
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee', 99 );

function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $meta_items = array();
    if( !empty( $cart_data ) ) {
        $meta_items = $cart_data; // <- ? missing error ?
    }
    if( isset( $cart_item["add_num_fee_one"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "1");
    }
    if( isset( $cart_item["add_num_fee_two"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "2");
    }
    if( isset( $cart_item["add_num_fee_three"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "3");
    }
    if( isset( $cart_item["add_num_fee_four"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "4");
    }
    if( isset( $cart_item["add_num_fee_five"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "5");
    }
    if( isset( $cart_item["add_num_fee_six"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "6");
    }
    if( isset( $cart_item["add_num_fee_seven"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "7");
    }
    if( isset( $cart_item["add_num_fee_eight"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "8");
    }
    if( isset( $cart_item["add_num_fee_nine"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "9");
    }
    if( isset( $cart_item["add_num_fee_ten"] ) ) {
        $meta_items[] = array( "name" => "Numero Giorni", "value" => "10");
    }
    return $meta_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 99, 2 );

function days_order_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_one"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '1');
    }
    if( isset( $values["add_num_fee_two"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '2');
    }
    if( isset( $values["add_num_fee_thre"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '3');
    }
    if( isset( $values["add_num_fee_four"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '4');
    }
    if( isset( $values["add_num_fee_five"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '5');
    }
    if( isset( $values["add_num_fee_six"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '6');
    }
    if( isset( $values["add_num_fee_seven"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '7');
    }
    if( isset( $values["add_num_fee_eight"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '8');
    }
    if( isset( $values["add_num_fee_nine"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '9');
    }
    if( isset( $values["add_num_fee_ten"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '10');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_meta_handler', 99, 3);

Any suggestion? Many thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source