从所有WooCommerce预订中获取所有人员的总和 [英] Get all persons as a sum from all WooCommerce bookings

查看:41
本文介绍了从所有WooCommerce预订中获取所有人员的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了官方的woocommerce预订插件,并且尝试获取所有已预订产品的人数.

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

推荐答案

要获取所有完整"信息,预订人数计数使用以下内容:

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() );

简单地:

    $count   = array_sum( get_post_meta($booking_id, '_booking_persons', true) );

这篇关于从所有WooCommerce预订中获取所有人员的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆