woommercace after checkout字段无效..当取消检查创建帐户时,它仍然要求填写完整的帐户并进行验证



woommercace后签出字段没有验证。。。当取消检查创建帐户时,它仍然要求完全填充和验证错误我想要当我检查创建帐户的时候,这个验证将工作
在取消检查之前为什么这是要求验证希望得到我的点

功能

public function wooc_create_dobc_fields() {
$birthday_label    = get_option( 'label_birthday' );
$birthday_required = get_option( 'required_fields_dobc' );
if ( empty( $birthday_label ) ) {
$birthday_label = 'Enter Your Date Of Birthday';
}   
$user_id   = get_current_user_id();
$show_date = get_user_meta( $user_id, 'dobc_date_field', true );
get_user_meta( $user_id, 'dobc_date_field', true );
?>
<div class="create-account">
<p>
<label for="label_birthday"><?php echo esc_attr( $birthday_label, 'dobc' ); ?>
<?php if ( 'yes' === $birthday_required ) : ?>
<span class="required" style="color:red;">*</span>
<?php endif; ?>
</label>
<?php $required = ( 'yes' === $birthday_required ) ? 'required' : ''; ?>
<input type="date" class="input-text" name="dobc_date_field" id="date of birthd " value="<?php echo esc_html( $show_date ); ?>"  <?php echo esc_html( $required ); ?> />
<?php wp_nonce_field( 'birthday_nonce_action_checkout', 'birday_fields_checkout_nonce' ); ?>
</p>
</div>
<div class="clear"></div>
<?php
}

挂钩

add_action( 'woocommerce_register_form', array( $this, 'wooc_create_dobc_fields' ) );
add_action( 'woocommerce_checkout_process', array( $this,'my_custom_checkout_field_process' ) );

回叫验证

public function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( isset($_POST['dobc_date_field']  ) && empty($_POST['dobc_date_field'] )) 
wc_add_notice( __( 'Please Enter Date Of Birthday' ), 'error' );
}

我从团队领导那里得到了解决方案挂钩

if ( 'yes' === $birthday_required ) {
if ( 'yes' == get_option( 'woocommerce_enable_myaccount_registration' ) ) {
add_action( 'woocommerce_checkout_process', array( $this, 'my_custom_checkout_field_process' ) );

回叫功能

public function my_custom_checkout_field_process() {
if ( isset( $_POST['birday_fields_checkout_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['birday_fields_checkout_nonce'], 'birthday_nonce_action_checkout' ) ) )
) {
print 'Sorry, your nonce did not verify.';
exit;
}
// Check if set, if its not set add an error.
if ( isset( $_POST['createaccount'] ) && empty( $_POST['dobc_date_field'] ) ) {
wc_add_notice( __( 'Please Enter Date Of Birthday' ), 'error' );
}
}

最新更新