我正在使用WooCommerce预订进行课程注册。客户应添加为其预订座位的所有人员的姓名。我设法将名称保存为一个字符串,以按顺序排列。
这是我到目前为止的代码:
// Check cart for courses with attendees
function foobar_check_courses_in_cart ($checkout) {
global $woocommerce;
$courses_in_cart = array();
foreach ( $woocommerce->cart->get_cart() as $cart_item => $values ) {
$_courseid = $values['data']->get_id();
$_product = wc_get_product( $values['data']->get_id());
$_persons = array_sum($values['booking']['_persons']);
$_date = $values['booking']['date'];
if ($_persons > 0) {
$courses_in_cart[$_product->get_title()] = [$_persons, $_date, $_courseid];
} else {
$courses_in_cart[$_product->get_title()] = false;
}
}
return $courses_in_cart;
}
// Create name fields for every course and every attendee
function foobar_add_attendees_fields ($checkout) {
$courses_in_cart = foobar_check_courses_in_cart ($checkout);
foreach ($courses_in_cart as $course_name => $course_details) {
if ( $course_details ) {
// (...)
for ($i=1; $i <= $course_details[0]; $i++) {
echo '<div class="col-md-12"><b>Attendee '. $i .'</b></div><div class="col-md-4">';
woocommerce_form_field( 'foobar_attendees['. $course_details[2] .']['. $i .'][firstname]', array(
'type' => 'text',
'required' => 'true',
'input_class' => array( 'form-control' ),
'placeholder' => __( 'Vorname' ),
), $checkout->get_value( 'foobar_attendees['. $course_details[2] .']['. $i .'][firstname]' ) );
echo '</div>';
echo '<div class="col-md-4">';
woocommerce_form_field( 'foobar_attendees['. $course_details[2] .']['. $i .'][name]', array(
'type' => 'text',
'required' => 'true',
'input_class' => array( 'form-control' ),
'placeholder' => __( 'Nachname' ),
), $checkout->get_value( 'foobar_attendees['. $course_details[2] .']['. $i .'][name]' ) );
echo '</div>';
echo '<div class="col-md-4">';
woocommerce_form_field( 'foobar_attendees['. $course_details[2] .']['. $i .'][age]', array(
'type' => 'text',
'required' => 'true',
'input_class' => array( 'form-control' ),
'placeholder' => __( 'Alter' ),
), $checkout->get_value( 'foobar_attendees['. $course_details[2] .']['. $i .'][age]' ) );
echo '</div>';
}
echo '</div>';
}
}
echo '</div>';
}
add_action( 'woocommerce_after_order_notes', 'foobar_add_attendees_fields');
// Validate attendee fields
function foobar_validate_attendees_fields() {
// (...)
}
add_action('woocommerce_checkout_process', 'foobar_validate_attendees_fields');
// Save everything as a string in order meta (this is bad!)
function foobar_save_attendees_fields( $order_id ) {
if ( $_POST['foobar_attendees'] ) {
$foobar_attendees_text = '';
foreach ($_POST['foobar_attendees'] as $course => $attendees) {
$foobar_course_name = wc_get_product(sanitize_text_field($course))->get_name();
$foobar_attendees_text .= '<br>'. $foobar_course_name .':';
foreach ($attendees as $key => $value) {
$foobar_attendees_text .= '<br>'.$value['firstname'].' '.$value['name'].' ('.$value['age'].')';
}
$foobar_attendees_text .= '<br>';
}
update_post_meta( $order_id, 'Attendees', $foobar_attendees_text );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'foobar_save_attendees_fields' );
// Display attendees in wp-admin/orders/123
function foobar_display_attendees_fields_admin($order){
echo '<p><strong>'.__('Attendees').':</strong> ' . get_post_meta( $order->id, 'Attendees', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'foobar_display_attendees_fields_admin', 10, 1 );
// Display attendees in mails
function foobar_display_attendees_fields_email( $keys ) {
$keys[] = 'Attendees';
return $keys;
}
add_filter('woocommerce_email_order_meta_keys', 'foobar_display_attendees_fields_email');
这是结果:
每个订单包含一个不同课程/预订的参与者的字符串很痛苦,而且对于出口等来说很糟糕。所以我想为每次预订单独保存参与者。
在 Booking 1 详细信息页面
上参加者_1:名字姓氏(年龄)
与会者_2:名姓(年龄) ...
。
在 Booking 2 详细信息页面
上...
如何将每个与会者添加到单个预订中?我可以以某种方式update_post_meta
但有预订ID吗?
我们非常感谢您的帮助!