Documentation

Add these snippets to your child theme’s functions.php file or use a plugin like Code snippets. Don’t add code directly to the parent theme’s functions.php file as it will be overridden by updates. Customize the texts/values in red to whatever you need.

Make sure all items have the same date(s) in cart

add_action( 'woocommerce_check_cart_items', 'wceb_check_dates_in_cart', 10, 1 );

function wceb_check_dates_in_cart() {

    $i = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {

        if ( isset( $values['_booking_start_date'] ) ) {

            $start = $values['_booking_start_date'];
            $end = isset( $values['_booking_end_date'] ) ? $values['_booking_end_date'] : $start;

            if ( $i === 0 ) {

                $start_date = $start;
                $end_date = $end;

            } else if ( $start !== $start_date || $end !== $end_date ) {

                wc_add_notice( __( 'You must select the same dates for all your items.', 'woocommerce-easy-booking-system' ), 'error' );
                return false;

            }

            $i++;

        }
    }

    return true;

}

Display booking duration in cart

add_filter( 'woocommerce_get_item_data', 'wceb_get_custom_item_data', 10, 2 );

function wceb_get_custom_item_data( $other_data, $cart_item ) {

    if ( isset( $cart_item['_booking_duration'] ) && ! empty ( $cart_item['_booking_duration'] ) ) {

        $booking_mode = get_option( 'wceb_booking_mode' );
        $unit = $booking_mode === 'nights' ? _n( 'night', 'nights', $cart_item['_booking_duration'], 'woocommerce-easy-booking-system' ) : _n( 'day', 'days', $cart_item['_booking_duration'], 'woocommerce-easy-booking-system' );

        $other_data[] = array(
            'name' => 'Booking duration',
            'value' => esc_html( $cart_item['_booking_duration'] . ' ' . $unit )
        );

    }

    return $other_data;

}

Display booking duration in checkout

add_action( 'woocommerce_checkout_create_order_line_item', 'wceb_add_custom_order_item_meta', 10, 3 );

function wceb_add_custom_order_item_meta( $item, $cart_item_key, $values ) {

    if ( ! empty( $values['_booking_duration'] ) ) {
        $item->add_meta_data( '_booking_duration', absint( $values['_booking_duration'] ) );
    }

}

add_filter( 'easy_booking_display_item_meta', 'wceb_display_custom_order_item_meta', 10, 2 );

function wceb_display_custom_order_item_meta( $meta_data, $item ) {

    $booking_duration = $item->get_meta( '_booking_duration' );

    if ( isset( $booking_duration ) && ! empty( $booking_duration ) ) {

        $booking_mode = get_option( 'wceb_booking_mode' );
        $unit = $booking_mode === 'nights' ? _n( 'night', 'nights', $booking_duration, 'woocommerce-easy-booking-system' ) : _n( 'day', 'days', $booking_duration, 'woocommerce-easy-booking-system' );

        $meta_data[] = array(
            'display_key'   => esc_html( 'Booking duration' ),
            'display_value' => esc_html( $booking_duration . ' ' . $unit )
        );

    }

    return $meta_data;

}