Arrays Woocommerce 3中的自定义签出字段验证

Arrays Woocommerce 3中的自定义签出字段验证,arrays,woocommerce,checkout,custom-fields,woocommerce-bookings,Arrays,Woocommerce,Checkout,Custom Fields,Woocommerce Bookings,我在预订网站的结帐页面上创建了自定义输入字段。输入字段的数量基于人员计数。我已经设法将它们显示在结帐表单上,对它们进行消毒,并显示在订单管理面板中。输入字段是必需的,因此如果其中一个或多个字段为空,则会出现错误(wc_add_notice)。我如何才能获得te wc_添加_通知?这是我的密码: //* Add a new checkout field function ppp_filter_checkout_fields($fields){ $fields['extra_fields'

我在预订网站的结帐页面上创建了自定义输入字段。输入字段的数量基于人员计数。我已经设法将它们显示在结帐表单上,对它们进行消毒,并显示在订单管理面板中。输入字段是必需的,因此如果其中一个或多个字段为空,则会出现错误(wc_add_notice)。我如何才能获得te wc_添加_通知?这是我的密码:

//* Add a new checkout field 
function ppp_filter_checkout_fields($fields){
    $fields['extra_fields'] = array(
    'participant_details' => array(
        'type' => 'participant_details',
        'required'      => true,
        'label' => __( 'Participant Details' )
        ),
    );

    return $fields;

}
add_filter( 'woocommerce_checkout_fields', 'ppp_filter_checkout_fields' );

//* Add the field to the checkout
function ppp_filter_checkout_field_group( $field, $key, $args, $value ){
    $op_cart_count = WC()->cart->get_cart_contents_count();

    $items = WC()->cart->get_cart();

    foreach($items as $item) { 
        $person = $item['booking']['_persons'][0];
    }

    if ($person > 1) {

        $html = '';

        $html .= "<h3>Deelnemers</h3>";

        for ( $i = 1; $i < $person; $i++) {
            $counter = $i + 1;

            $html .= "Deelnemer ". $counter . "<br>";

            $html .= woocommerce_form_field( "participant_details[$i][full_name]", array(
                "type" => "text",
                "return" => true,
                "value" => "",
                "required"      => true,
                "label" => __( "Naam" )
                )
            );
            $html .= woocommerce_form_field( "participant_details[$i][email_address]", array(
                "type" => "text",
                "return" => true,
                "value" => "",
                "required"      => true,
                "label" => __( "Emailadres" )
                )
            );

        }
        return $html;

    }
}
add_filter( 'woocommerce_form_field_participant_details', 'ppp_filter_checkout_field_group', 10, 4 );

//* display the extra field on the checkout form
function ppp_extra_checkout_fields(){ 

    $checkout = WC()->checkout();

    foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) :

        woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );

    endforeach;


}
add_action( 'woocommerce_checkout_after_customer_details' ,'ppp_extra_checkout_fields' );

//* Sanitize our custom field
function ppp_custom_process_checkout_field_participant_details( $posted ){

    $clean = array();

    foreach( $posted as $participant ){
        $details = ppp_custom_checkout_clean_participant_details( $participant );

        if( ! empty( $details ) ){
            $clean[] = $details;
        }
    }

    return $clean;
}
add_filter( 'woocommerce_process_checkout_participant_details_field', 'ppp_custom_process_checkout_field_participant_details' );

//*
function ppp_custom_checkout_clean_participant_details( $participant = array() ){
    $details = array();
    if( isset( $participant["full_name"] ) ){
        $details['full_name'] = sanitize_text_field( $participant["full_name"] );
    }
    if( isset( $participant["email_address"] ) ){
        $details['email_address'] = sanitize_text_field( $participant["email_address"] );
    }
    return $details;
}

//* Update_post_meta
function ppp_custom_checkout_field_update_order_meta( $order_id, $posted ){

    if( ! empty( $posted["participant_details"] ) ){
        update_post_meta( $order_id, "_participant_details", $posted["participant_details"] );
    } else {
        delete_post_meta( $order_id, "_participant_details" );
    }

}
add_action( 'woocommerce_checkout_update_order_meta', 'ppp_custom_checkout_field_update_order_meta', 10, 2 );

//* Display the extra data in the order admin panel
function ppp_display_order_data_in_admin( $order ){  

    $participant_details = get_post_meta( $order->id, "_participant_details", true ); 

    if( ! empty( $participant_details ) ) { 

        $participant_defaults = array(
                "full_name" => "",
                "email_address" => ""
            );

    ?>
    <div class="participant_data">
        <h4><?php _e( "Deelnemers Lijst", "prespersadprodukties" ); ?></h4>
        <?php 
            $i = 1;

            foreach( $participant_details as $participant ){

                $participant = wp_parse_args( $participant, $participant_defaults );

                echo "<p><strong>" . sprintf( __( "Deelnemer %s", "prespersadprodukties" ), $i+1  ) . "</strong>" . "<br/>";
                echo __( "Naam", "prespersadprodukties" ) . ' : ' . $participant["full_name"] . "<br/>";
                echo __( "Emailadres", "prespersadprodukties" ) . ' : ' . $participant["email_address"] . "<br/>";
                echo "</p>";

                $i++;

            }

         ?>
    </div>
<?php }
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'ppp_display_order_data_in_admin' );
//*添加新的签出字段
函数ppp\u过滤器\u签出\u字段($fields){
$fields['extra_fields']=数组(
“参与者详细信息”=>数组(
'类型'=>'参与者详细信息',
“必需”=>true,
“标签”=>\(“参与者详细信息”)
),
);
返回$fields;
}
添加_过滤器('woocommerce_checkout_fields'、'ppp_filter_checkout_fields');
//*将该字段添加到签出
函数ppp\u filter\u checkout\u field\u group($field、$key、$args、$value){
$op_cart_count=WC()->cart->get_cart_contents_count();
$items=WC()->cart->get_cart();
foreach($items作为$item){
$person=$item['booking']['u persons'][0];
}
如果($person>1){
$html='';
$html.=“Deelnemers”;
对于($i=1;$i<$person;$i++){
$counter=$i+1;
$html.=“Deelnemer”。$counter.
”; $html.=woocommerce\u form\u字段(“参与者详细信息[$i][全名]”,数组( “键入”=>“文本”, “return”=>true, “值”=>“”, “必需”=>true, “标签”=>(Naam) ) ); $html.=woocommerce\u表单\u字段(“参与者详细信息[$i][电子邮件地址]”,数组( “键入”=>“文本”, “return”=>true, “值”=>“”, “必需”=>true, “标签”=>(电子邮件地址) ) ); } 返回$html; } } 添加过滤器('woocommerce\u form\u field\u participant\u details'、'ppp\u filter\u checkout\u field\u group',10,4); //*在签出窗体上显示额外字段 函数ppp_extra_checkout_fields(){ $checkout=WC()->checkout(); foreach($checkout->checkout\u字段['extra\u字段]]作为$key=>$field): woocommerce_表单_字段($key,$field,$checkout->get_value($key)); endforeach; } 添加_操作('woocommerce_checkout_在_customer_details'之后,'ppp_extra_checkout_字段'); //*清理自定义字段 功能ppp\自定义\流程\签出\字段\参与者\详细信息($posted){ $clean=array(); foreach($作为参与者过帐){ $details=ppp\自定义\结帐\清洁\参与者\详细信息($participant); 如果(!空($details)){ $clean[]=$details; } } 退回$clean; } 添加过滤器('woocommerce\u process\u checkout\u participant\u details\u field'、'ppp\u custom\u process\u checkout\u field\u participant\u details'); //* 函数ppp\自定义\签出\清理\参与者\详细信息($participant=array()){ $details=array(); if(isset($participant[“全名”)){ $details['full_name']=清理文本字段($participant[“full_name”); } if(isset($participant[“email_address”])){ $details['email\u address']=清理文本字段($participant[“email\u address]”); } 返回$details; } //*更新\u post\u meta 功能ppp\自定义\签出\字段\更新\订单\元($order\ id,$posted){ 如果(!空($posted[“参与者详细信息”])){ 更新发布元($order\u id,“\u participant\u details”,$posted[“participant\u details”]); }否则{ 删除帖子元($order\u id,“\u participant\u details”); } } 添加操作('woocommerce\u checkout\u update\u order\u meta'、'ppp\u custom\u checkout\u field\u update\u order\u meta',10,2); //*在订单管理面板中显示额外数据 函数ppp\u显示\u顺序\u数据\u在\u管理($order){ $participant\u details=get\u post\u meta($order->id,“\u participant\u details”,true); 如果(!empty($participant_details)){ $participant\u默认值=数组( “全名”=>“”, “电子邮件地址”=>“ ); ?>
已更新

注意:您的代码仅在购物车中有一个唯一的项目时有效……如果您有两个不同的预订项目,它将只处理最后添加的一个项目

现在,为了进行验证,我们首先需要添加一个隐藏的输入字段,其中包含人数。因此,我对您的第一个挂钩函数做了一些修改:

// Add a new checkout field
add_filter( 'woocommerce_checkout_fields', 'ppp_filter_checkout_fields' );
function ppp_filter_checkout_fields($fields){

    $fields['extra_fields'] = array(
    'participant_details' => array(
        'type' => 'participant_details',
        'required'      => false,
        'label' => __( 'Participant Details' )
        ),
    );

    // Add a "persons" hidden input field
    foreach( WC()->cart->get_cart() as $cart_item ) {
        $persons = $cart_item['booking']['_persons'][0];
    }
    echo '<input type="hidden" name="persons" value="' . $persons . '">';

    return $fields;
}
//添加新的签出字段
添加_过滤器('woocommerce_checkout_fields'、'ppp_filter_checkout_fields');
函数ppp\u过滤器\u签出\u字段($fields){
$fields['extra_fields']=数组(
“参与者详细信息”=>数组(
'类型'=>'参与者详细信息',
“必需”=>false,
“标签”=>\(“参与者详细信息”)
),
);
//添加“人员”隐藏输入字段
foreach(WC()->cart->get_cart()作为$cart_项目){
$persons=$cart_项目['booking']['u persons'][0];
}
回声';
返回$fields;
}
然后,在下面的代码中,我们可以轻松获得人数,并对自定义签出字段进行验证:

//* Validate custom checkout fields
add_action( 'woocommerce_after_checkout_validation', 'ppp_checkout_field_participant_details_validation', 20, 2 );
function ppp_checkout_field_participant_details_validation( $data, $errors ){
    if ( isset( $_POST['persons'] ) && $_POST['persons'] > 1 ){

        for ( $i = 1, $j = 2; $i < esc_attr( $_POST['persons'] ); $i++, $j++ ) {
            if ( empty( $_POST['participant_details'][$i]['full_name'] ) )
                $errors->add( 'participant_'.$i.'_full_name',  __( "Please fill in the participant $j full name" ), 'error' );

            if ( empty( $_POST['participant_details'][$i]['email_address'] ) )
                $errors->add( 'participant_'.$i.'_email_address',  __( "Please fill in the participant $j email address" ), 'error' );
        }
    }
}
//*验证自定义签出字段
添加行动('woocommerce\u在\u签出\u验证之后,'ppp\u签出\u字段\u参与者\u详细信息\u验证',20,2);
功能ppp检查字段参与者详细信息验证($data$errors){
如果(isset($_POST['persons'])和&$_POST['persons']>1){
对于($i=1,$j=2;$iadd('participant_uu'.$i.'u full_uname',uu(“请填写参与者$j全名”),'error');
如果(空($_POST['participant\u details'][$i]['email\u address']))