skip to Main Content

I use the official woocommerce booking plugin and I try do get the quantity of all persons that have booked a product.

for a single order that’s no problem with:

if ( is_callable( 'WC_booking_Data_Store::get_booking_ids_from_order_id') ) {
        $booking_data = new WC_booking_Data_Store();
        $booking_ids = $booking_data->get_booking_ids_from_order_id( $order->get_id() );
    }
            foreach ( $booking_ids as $booking_id ) {
            $booking = new WC_booking( $booking_id );
            
            $person_count = array_sum( $booking->get_person_counts() );
            $total_person_count .= 'Booking id: ' . $booking_id . ' Person Count: ' . $person_count . ' ';
        }

but how can I collect the sum of all bookings? Hope you can help me

2

Answers


  1. following your updated code, please try this

    <?php
    if ( is_callable( 'WC_booking_Data_Store::get_booking_ids_from_order_id') ) {
        $booking_data = new WC_booking_Data_Store();
        $booking_ids = $booking_data->get_booking_ids_from_order_id( $order->get_id() );
    }
    $total_count_data = array();
    $total_count_persons = 0;
    foreach ( $booking_ids as $booking_id ) {
        $booking = new WC_booking( $booking_id );
        $person_count = array_sum( $booking->get_person_counts() );
        $total_count_persons+= $person_count;
        $total_count_data[] = array(
                'Booking'   => $booking_id,
                'Person'    => $person_count
            );
    }
    
    Login or Signup to reply.
  2. To get all "complete" bookings persons count use the following:

    // get all bookings Ids with a status "complete"
    $bookings_ids = get_posts( array(
        'posts_per_page' => -1,
        'post_type'      => 'wc_booking', // booking post type
        'post_status'    => 'complete',
        'fields'         => 'ids',
    ));
    
    $persons_count = 0; // Initializing
    $persons_html  = '<table><tr><th>Booking id</th><th>Persons count</th></tr>'; // Initializing
    
    // Loop through booking Ids
    foreach ( $bookings_ids as $booking_id ) {
        $booking = new WC_Booking( $booking_id ); // Get the WC_Booking instance object
        $count   = array_sum( $booking->get_person_counts() );
    
        $persons_count += $count;
        $persons_html  .= '<tr><td>' . $booking_id . '</td><td>' . $count . '</td></tr>';
    }
    $persons_html .= '<tr><th><strong>Total count</th><td style="color:red">' . $persons_count . '</td></tr>';
    
    // Output
    echo $persons_html . '</table>';
    

    Tested and works.

    To make it lighter, you could replace:

        $booking = new WC_Booking( $booking_id ); // Get the WC_Booking instance object
        $count   = array_sum( $booking->get_person_counts() );
    

    simply by:

        $count   = array_sum( get_post_meta($booking_id, '_booking_persons', true) );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search