php - 从 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();
}
}
测试和工作。
推荐阅读
- c++ - 添加 static_pointer_cast
到 std::list > 导致纯虚方法调用错误 - haskell - 这个错误中的 IHaskellPrelude 来自哪里?
- angular - 在 LocalStorage 中存储令牌
- typescript - 有没有办法在量角器中重新运行失败的套件?
- python - boto3 使用实例 ID 创建图像
- plsql - ORA-01722: 无效号码 ORA-06512: 在第 17 行 01722. 00000 - “无效号码”
- mysql - 如何在mysql中解决这个查询?
- javascript - HTML,当内容达到一定高度时如何制作可重复的页眉和页脚?
- ibm-mq - IBM MQ 使用 amqsgetc 失败,原因为“2058”
- html - 如何刷新 Aurelia 中的子组件以撤消表单?