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

时间:2018-07-26 14:23:27

标签: php wordpress woocommerce cart woocommerce-bookings

我已启用Woocommerce Bookings插件,我需要在结帐页面上显示添加到购物车的人员类型。

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

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

1 个答案:

答案 0 :(得分:1)

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

经过测试可以正常工作。

相关问题