我在结帐页面中保存一系列条件自定义字段时遇到了困难:它们在页面上工作,但未保存在order_meta中。问题肯定在于字段名称,因为它们的数量越来越多。
实际上,在结账表格中,客户被询问他/她是否与一群人一起旅行(即,如果订单是针对> 1旅行套餐的话)。如果答案是肯定的(第一个自定义字段),则必须输入与客户一起旅行的人员(名字,姓氏,电子邮件和电话)。
我创建了4个条件字段,通过"而#34;使用递增计数器循环:计数器还用于定义唯一字段的名称,如下所示。
add_action( 'woocommerce_after_order_notes', 'travelling_in_group' );
function travelling_in_group( $checkout ) {
echo '<div id="travelling-in-group"><h3>' . __('Are you paying for a CSR or IDP cruise?') . '</h3>';
echo '<br/>';
echo '<p> If you are paying not only for yourself, but also for other people travelling with you, please write in the text below contact details of the persons part of the group</p>';
echo '<h5>Are you tavelling in group?</h5>';
woocommerce_form_field( 'travel_group', array(
'type' => 'checkbox',
'class' => array('conditional-checkbox form-row-wide'),
'label' => __('Yes'),
), $checkout->get_value( 'travel_group' ));
$quantity_cond = quantity_conditional();
echo ' <p class="checkout-custom-conditional">Group participants details </p>';
if ($quantity_cond >1) {
$i=1;
while ($i<$quantity_cond) {
woocommerce_form_field( 'first_name_group_' . $i, array(
'type' => 'text',
'class' => array('form-row-first checkout-custom-conditional'),
'placeholder' => __('First Name'),
'label' => __('First Name'),
), $checkout->get_value( 'first_name_group_' . $i ));
woocommerce_form_field( 'last_name_group_' . $i, array(
'type' => 'text',
'class' => array('form-row-last checkout-custom-conditional'),
'placeholder' => __('Last Name'),
'label' => __('Last Name'),
), $checkout->get_value( 'last_name_group_' . $i ));
woocommerce_form_field( 'email_group_' . $i, array(
'type' => 'text',
'class' => array('form-row-last checkout-custom-conditional'),
'placeholder' => __('Email'),
'label' => __('Email'),
), $checkout->get_value( 'email_group_' . $i ));
woocommerce_form_field( 'phone_group_' . $i, array(
'type' => 'text',
'class' => array('form-row-last checkout-custom-conditional'),
'placeholder' => __('Phone'),
'label' => __('Phone'),
), $checkout->get_value( 'phone_group_' . $i ));
$i++;
}
}
echo '</div>';
}
//check for conditional
function quantity_conditional() {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values) {
$quantity = $values['quantity'];
if ($quantity >1)
{
$quantity_cart = $quantity;
}
}
return $quantity_cart;
}
以下代码在结帐页面上运行正常,但以下代码更新order_meta并不起作用,只创建4个字段(名字/姓氏/电子邮件/电话),其中没有任何内容
//Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'travelling_in_group_order_meta' );
function travelling_in_group_order_meta( $order_id ) {
if ( ! empty( $_POST['travel_group'] ) ) {
update_post_meta( $order_id, 'First Name person_'.$i .'', sanitize_text_field( $_POST['first_name_group_' . $i] ) );
update_post_meta( $order_id, 'Last Name person_'.$i .'', sanitize_text_field( $_POST['last_name_group_' . $i] ) );
update_post_meta( $order_id, 'Email person_'.$i .'', sanitize_text_field( $_POST['email_group_' . $i] ) );
update_post_meta( $order_id, 'Phone person_'.$i .'', sanitize_text_field( $_POST['phone_group_' . $i] ) );
}
}
您对如何解决此问题有任何建议吗?谢谢!