首页 > 解决方案 > 从 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')
        )
    );
}

标签: phpwordpresswoocommercecartwoocommerce-bookings

解决方案


要从 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();
    }
}

测试和工作。


推荐阅读