Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 suggestions guys ?
Many thanks

What I have tried:

<pre>//
// ****************************
// ******* WooC SUPPORT *******
// ****************************
//

// Enable extra variable
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_one( $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";
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_one', 99, 2 );
    
function save_booking_variations_two( $cart_item_data, $product_id ){

    if( isset( $_POST['option_num_day_two'] ) &&
    $_POST['option_num_day_two'] === '2' ) {
        $cart_item_data[ "add_num_fee_two" ] = "2";
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_two', 99, 2 );
    
function save_booking_variations_three( $cart_item_data, $product_id ){
    
    if( isset( $_POST['option_num_day_three'] ) &&
    $_POST['option_num_day_three'] === '3' ) {
        $cart_item_data[ "add_num_fee_three" ] = "3";
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_three', 99, 2 );

function save_booking_variations_four( $cart_item_data, $product_id ){

    if( isset( $_POST['option_num_day_four'] ) &&
    $_POST['option_num_day_four'] === '4' ) {
        $cart_item_data[ "add_num_fee_four" ] = "4";
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_four', 99, 2 );
    
function save_booking_variations_five( $cart_item_data, $product_id ){

    if( isset( $_POST['option_num_day_five'] ) &&
    $_POST['option_num_day_five'] === '5' ) {
        $cart_item_data[ "add_num_fee_five" ] = "5";
    }
        return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_five', 99, 2 );

function save_booking_variations_six( $cart_item_data, $product_id ){

    if( isset( $_POST['option_num_day_six'] ) &&
    $_POST['option_num_day_six'] === '6' ) {
        $cart_item_data[ "add_num_fee_six" ] = "6";
    }
        return $cart_item_data;

}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_six', 99, 2 );

function save_booking_variations_seven( $cart_item_data, $product_id ){

    if( isset( $_POST['option_num_day_seven'] ) &&
    $_POST['option_num_day_seven'] === '7' ) {
        $cart_item_data[ "add_num_fee_seven" ] = "7";
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_seven', 99, 2 );

function save_booking_variations_eight( $cart_item_data, $product_id ){

    if( isset( $_POST['option_num_day_eight'] ) &&
    $_POST['option_num_day_eigh'] === '8' ) {
        $cart_item_data[ "add_num_fee_eight" ] = "8";
    }
        return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_eight', 99, 2 );

function save_booking_variations_nine( $cart_item_data, $product_id ){

    if( isset( $_POST['option_num_day_nine'] ) &&
    $_POST['option_num_day_nine'] === '9' ) {
        $cart_item_data[ "add_num_fee_nine" ] = "9";
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_booking_variations_nine', 99, 2 );

function save_booking_variations_ten( $cart_item_data, $product_id ){

    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_ten', 99, 2 );


// ############
// ############
// ############

function calculate_num_days_fee_one( $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);
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_one', 99 );

function calculate_num_days_fee_two( $cart_object ) {
    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);
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_two', 99 );


function calculate_num_days_fee_three( $cart_object ) {

    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);
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_three', 99 );


function calculate_num_days_fee_four( $cart_object ) {
    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);
                }
            }
        }
    }
}

add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_four', 99 );

function calculate_num_days_fee_five( $cart_object ) {
    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);
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_five', 99 );

function calculate_num_days_fee_six( $cart_object ) {
    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);
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_six', 99 );

function calculate_num_days_fee_seven( $cart_object ) {
    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);
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_seven', 99 );

function calculate_num_days_fee_eight( $cart_object ) {
    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);
                }
            }
        }
    }
}    
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_eight', 99 );

function calculate_num_days_fee_nine( $cart_object ) {
    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);
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_num_days_fee_nine', 99 );

function calculate_num_days_fee_ten( $cart_object ) {
    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_ten', 99 );

// ############
// ############
// ############

function render_meta_one_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $meta_items_one = array();
    if( !empty( $cart_data ) ) {
        $meta_items_one = $cart_data; // <- ? missing error ?
    }
    if( isset( $cart_item["add_num_fee_one"] ) ) {
        $meta_items_one[] = array( "name" => "Numero Giorni", "value" => "1");
    }
    return $meta_items_one;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_one_on_cart_and_checkout', 99, 2 );

function render_meta_two_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $meta_items_two = array();
    if( !empty( $cart_data ) ) {
        $meta_items_two = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item["add_num_fee_two"] ) ) {
        $meta_items_two[] = array( "name" => "Numero Giorni", "value" => "2");
    }

        return $meta_items_two;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_two_on_cart_and_checkout', 99, 2 );

function render_meta_three_on_cart_and_checkout( $cart_data, $cart_item = null ) {

    $meta_items_three = array();
    if( !empty( $cart_data ) ) {
        $meta_items_three = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item_three["add_num_fee_three"] ) ) {
        $meta_items_three[] = array( "name" => "Numero Giorni", "value" => "3");
    }
        return $meta_items_three;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_three_on_cart_and_checkout', 99, 2 );

function render_meta_foue_on_cart_and_checkout( $cart_data, $cart_item = null ) {

    $meta_items_foue = array();
    if( !empty( $cart_data ) ) {
        $meta_items_foue = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item_foue["add_num_fee_foue"] ) ) {
        $meta_items_foue[] = array( "name" => "Numero Giorni", "value" => "4");
    }
        return $meta_items_foue;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_foue_on_cart_and_checkout', 99, 2 );

function render_meta_five_on_cart_and_checkout( $cart_data, $cart_item = null ) {

    $meta_items_five = array();
    if( !empty( $cart_data ) ) {
        $meta_items_five = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item_five["add_num_fee_five"] ) ) {
        $meta_items_five[] = array( "name" => "Numero Giorni", "value" => "5");
    }
        return $meta_items_five;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_five_on_cart_and_checkout', 99, 2 );

function render_meta_six_on_cart_and_checkout( $cart_data, $cart_item = null ) {

    $meta_items_six = array();
    if( !empty( $cart_data ) ) {
        $meta_items_six = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item_six["add_num_fee_six"] ) ) {
        $meta_items_six[] = array( "name" => "Numero Giorni", "value" => "6");
    }
        return $meta_items_six;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_six_on_cart_and_checkout', 99, 2 );

function render_meta_seven_on_cart_and_checkout( $cart_data, $cart_item = null ) {

    $meta_items_seven = array();
    if( !empty( $cart_data ) ) {
        $meta_items_seven = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item_seven["add_num_fee_seven"] ) ) {
        $meta_items_seven[] = array( "name" => "Numero Giorni", "value" => "7");
    }
        return $meta_items_seven;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_seven_on_cart_and_checkout', 99, 2 );

function render_meta_eight_on_cart_and_checkout( $cart_data, $cart_item = null ) {

    $meta_items_eight = array();
    if( !empty( $cart_data ) ) {
        $meta_items_eight = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item_eight["add_num_fee_eight"] ) ) {
        $meta_items_eight[] = array( "name" => "Numero Giorni", "value" => "8");
    }
        return $meta_items_eight;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_eight_on_cart_and_checkout', 99, 2 );

function render_meta_nine_on_cart_and_checkout( $cart_data, $cart_item = null ) {

    $meta_items_nine = array();
    if( !empty( $cart_data ) ) {
        $meta_items_nine = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item_nine["add_num_fee_nine"] ) ) {
        $meta_items_nine[] = array( "name" => "Numero Giorni", "value" => "9");
    }
        return $meta_items_nine;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_nine_on_cart_and_checkout', 99, 2 );

function render_meta_ten_on_cart_and_checkout( $cart_data, $cart_item = null ) {

    $meta_items_ten = array();
    if( !empty( $cart_data ) ) {
        $meta_items_ten = $cart_data; // <- ? missing error ?
    }

    if( isset( $cart_item_ten["add_num_fee_ten"] ) ) {
        $meta_items_ten[] = array( "name" => "Numero Giorni", "value" => "10");
    }
        return $meta_items_ten;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_ten_on_cart_and_checkout', 99, 2 );


// ############
// ############
// ############


function days_order_one_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');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_one_meta_handler', 99, 3);

function days_order_two_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_two"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '2');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_two_meta_handler', 99, 3);

function days_order_three_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_three"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '3');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_three_meta_handler', 99, 3);

function days_order_four_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_four"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '4');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_four_meta_handler', 99, 3);

function days_order_five_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_five"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '5');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_five_meta_handler', 99, 3);

function days_order_six_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_six"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '6');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_six_meta_handler', 99, 3);

function days_order_seven_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_seven"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '7');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_seven_meta_handler', 99, 3);

function days_order_eight_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_eight"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '8');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_eight_meta_handler', 99, 3);

function days_order_nine_meta_handler( $item_id, $values, $cart_item_key ){
    if( isset( $values["add_num_fee_nine"] ) ){
        wc_add_order_item_meta( $item_id, "Numero Giorni", '9');
    }
}
add_action( 'woocommerce_add_order_item_meta', 'days_order_nine_meta_handler', 99, 3);

function days_order_ten_meta_handler( $item_id, $values, $cart_item_key ){
    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_ten_meta_handler', 99, 3);
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900