我使用官方 woocommerce 预订插件,我尝试获取所有已预订产品的人的数量。
对于单个订单来说没有问题:
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 . ' ';
}
但是我怎样才能收取所有预订的总和呢?希望你能帮助我
要获取所有“完整”预订人数,请使用以下方法:
// 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>';
已测试且有效。
为了让它更轻,你可以更换:
$booking = new WC_Booking( $booking_id ); // Get the WC_Booking instance object
$count = array_sum( $booking->get_person_counts() );
只需:
$count = array_sum( get_post_meta($booking_id, '_booking_persons', true) );
按照您更新的代码,请尝试这个
<?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
);
}
有没有办法将其用于每个日期和时间块? 要统计同一日期和时间的每个预订人数吗?