How to Display Dates or Other Attributes on the Checkout Page

Hi everyone,

I am currently using the RentalHive theme on my HivePress-based website and I need some assistance with showing specific attributes, such as dates, on the checkout page. I would be grateful if anyone could provide some guidance or suggestions on this topic. Here are my questions:

1- How can I modify the RentalHive theme to display booking dates on the checkout page? Are there any specific hooks or filters that I need to use for this purpose?

2- Is there an easy way to display custom attributes, such as rental item features, on the checkout page? Can I achieve this through theme customization or by customize any plugins?

I appreciate any help or suggestions from the community. Thanks in advance!

Hi Mustafa,

Sure, we can provide general guidance regarding customizations. Please let me know if you’re familiar with coding.

  1. The checkout page is implemented by WooCommerce so I recommend using their hooks. The booking ID is stored in the hp_booking meta of the purchased product, this way you can get the booking object and all the related attributes.

  2. Yes, if you get the booking object and you’re familiar with coding you can embed extra details in the checkout form.

Thank you fot your response! @ihor

Unfortunately, I cannot find the relevant data when I try the filter below. I need to get the booked dates. Can you tell me where I made a mistake by examining the function below for this?

function display_hp_booking_meta_on_checkout() {
    if ( ! is_checkout() ) {
        return;
    }

    $cart_items = WC()->cart->get_cart();

    foreach ( $cart_items as $cart_item ) {
        if ( isset( $cart_item['hp_booking'] ) ) {
            $booking_id = $cart_item['hp_booking'];
            $booking = get_post( $booking_id );
            ?>
            <pre>
                <?php var_dump($booking); ?>
            </pre>
            <?php
            if ( $booking ) {

                echo '<div class="hp-booking-meta">';
                echo '<h3>Rezervation Info:</h3>';
                echo '<p>Rezervation ID: ' . $booking_id . '</p>';
                echo '<p>Rezervation Created Date: ' . $booking->post_date . '</p>';
                echo '</div>';
            }
        }
    }
}
add_action( 'woocommerce_checkout_before_customer_details', 'display_hp_booking_meta_on_checkout' );


var_dump($booking) =>

object(WP_Post)#5754 (24) {
  ["ID"]=>
  int(1258)
  ["post_author"]=>
  string(1) "1"
  ["post_date"]=>
  string(19) "2023-05-05 17:07:34"
  ["post_date_gmt"]=>
  string(19) "2023-05-05 17:07:34"
  ["post_content"]=>
  string(0) ""
  ["post_title"]=>
  string(5) "#1258"
  ["post_excerpt"]=>
  string(0) ""
  ["post_status"]=>
  string(5) "draft"
  ["comment_status"]=>
  string(6) "closed"
  ["ping_status"]=>
  string(6) "closed"
  ["post_password"]=>
  string(0) ""
  ["post_name"]=>
  string(0) ""
  ["to_ping"]=>
  string(0) ""
  ["pinged"]=>
  string(0) ""
  ["post_modified"]=>
  string(19) "2023-05-05 17:07:34"
  ["post_modified_gmt"]=>
  string(19) "2023-05-05 17:07:34"
  ["post_content_filtered"]=>
  string(0) ""
  ["post_parent"]=>
  int(816)
  ["guid"]=>
  string(57) "https://domain.com/?post_type=hp_booking&p=1258"
  ["menu_order"]=>
  int(0)
  ["post_type"]=>
  string(10) "hp_booking"
  ["post_mime_type"]=>
  string(0) ""
  ["comment_count"]=>
  string(1) "0"
  ["filter"]=>
  string(3) "raw"
}

If you have a better suggestion that I have come up with a solution in this way, I would like to implement it.

function display_hp_booking_times_on_checkout() {
    if ( ! is_checkout() ) {
        return;
    }

    global $wpdb;

    $cart_items = WC()->cart->get_cart();

    foreach ( $cart_items as $cart_item_key => $cart_item ) {
        $booking_id = isset( $cart_item['hp_booking'] ) ? $cart_item['hp_booking'] : false;

        if ( $booking_id ) {
            $start_time_meta_key = 'hp_start_time';
            $end_time_meta_key = 'hp_end_time';

            $times_query = $wpdb->get_row(
                $wpdb->prepare(
                    "SELECT start_times.meta_value AS start_time, end_times.meta_value AS end_time
                    FROM {$wpdb->posts} AS bookings
                    INNER JOIN {$wpdb->postmeta} AS start_times ON ( bookings.ID = start_times.post_id AND start_times.meta_key = %s )
                    INNER JOIN {$wpdb->postmeta} AS end_times ON ( bookings.ID = end_times.post_id AND end_times.meta_key = %s )
                    WHERE bookings.ID = %d",
                    $start_time_meta_key,
                    $end_time_meta_key,
                    $booking_id
                ),
                ARRAY_A
            );

            if ( ! empty( $times_query ) ) {
                $start_time = date( 'F j, Y', $times_query['start_time'] );
                $end_time = date( 'F j, Y', $times_query['end_time'] );

                echo '<div class="hp-booking-times">';
                echo '<h3>Booking Times:</h3>';
                echo '<p>Start Time: ' . $start_time . '</p>';
                echo '<p>End Time: ' . $end_time . '</p>';
                echo '</div>';
            }
        }
    }
}
add_action( 'woocommerce_checkout_before_customer_details', 'display_hp_booking_times_on_checkout' );

Please try using this code to get the booking object by ID:

$booking=\HivePress\Models\Booking::query()->get_by_id($booking_id);

Then you can get any details this way:

echo $booking->display_dates();

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.