从 Woocommerce Bookings 中的购物车项目中获取人员类型名称

Get the person type name from cart items in Woocommerce Bookings

我启用了 Woocommerce 预订插件,我应该需要在结帐页面上显示添加到购物车中的人员类型。

我可以通过这段代码计算人数:

foreach(WC()->cart->get_cart() as $cart_item) { 
    $person = array_sum( $cart_item['booking']['_persons'] );
}

但是我需要显示人物类型名称

如何获得这些人的姓名?


如果我将您的代码添加到 for 循环中,它只需要一个人的类型名称。例如,我有 4 种人类型,如成人、青少年、儿童和婴儿。它只写婴儿。

for ( $i = 0; $i < $person; $i++) {
    $counter = $i + 1;

    $html .= " . $person_name . "; // Here

    $html .= woocommerce_form_field( "participant_details[$i][full_name]", array(
        "type" => "text",
        "return" => true,
        "value" => "",
        "class"         => array('form-row-first'),
        "required"      => false,
        "placeholder"       => __('Name')
        )
    );
}

要从 Woocommerce Bookings 获取可预订产品的购物车商品中的人名,请使用以下代码:

foreach(WC()->cart->get_cart() as $cart_item) {
    // The item persons count
    $person = array_sum( $cart_item['booking']['_persons'] );

    // Get the instance of the WC_Product_Booking product
    $booking = new WC_Product_Booking( $cart_item['product_id'] );

    // Loop through persons array to get the person names
    foreach ( $cart_item['booking']['_persons'] as $person_id => $person_qty ) {
        $person_name = $booking->get_person_types()[$person_id]->get_name();
    }
}

已测试并有效。