【问题标题】:Get all persons as a sum from all WooCommerce bookings从所有 WooCommerce 预订中获取所有人的总和
【发布时间】:2020-11-03 11:33:19
【问题描述】:

我使用官方的 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 . ' ';
        }

但是我怎样才能收集所有预订的总和?希望你能帮助我

【问题讨论】:

  • 您可以为bookings array 执行foreachfor 循环并将总和存储在$sum 变量中
  • 感谢您的回复。我已经试过了,但我没有得到所有的预订。这是我的代码:´´´
  • 您的代码未显示,请更新问题,并标记bookings 数组

标签: php wordpress woocommerce count woocommerce-bookings


【解决方案1】:

要计算所有“完整”预订人数,请使用以下方法:

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

【讨论】:

    【解决方案2】:

    按照您更新的代码,请试试这个

    <?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
            );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-25
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2015-05-10
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多