Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validating Custom Fields
<?php
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles' );
function child_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action('woocommerce_after_checkout_validation', 'digthis_two_step_checkout_validate', 9999, 2);
function digthis_two_step_checkout_validate($data, $errors) {
$step = filter_input(INPUT_POST, 'current_step');
if( empty( $errors->errors ) && $step == 'step-1' ){
$errors->add( 'digthis', __( '<span id="digthis-prevent-error">Digthis Error</span>', 'woocommerce' ) );
}
}
add_action('woocommerce_before_checkout_billing_form', 'customise_checkout_category_user', 0);
function customise_checkout_category_user($checkout) {
woocommerce_form_field( 'inscription_textbox_cf', array(
'type' => 'text',
'class' => array( 'inscription-text_cf form-row-wide' ),
'label' => __( 'Codice Fiscale' ),
'required' => true,
), $checkout->get_value( 'inscription_textbox_cf' ) );
woocommerce_form_field( 'inscription_textbox_iva', array(
'type' => 'text',
'class' => array( 'inscription-text_iva form-row form-row-last' ),
'label' => __( 'Partita Iva' ),
'required' => true,
), $checkout->get_value( 'inscription_textbox_iva' ) );
}
add_action('woocommerce_after_checkout_validation', 'validate_checkout_category_user', 10, 2);
function validate_checkout_category_user($data, $errors) {
$inscription_cf = filter_input(INPUT_POST, 'inscription_textbox_cf');
$inscription_iva = filter_input(INPUT_POST, 'inscription_textbox_cf');
if( $inscription_cf != 'digthis' ){
$errors->add( 'digthis', __( 'value should be digthis', 'woocommerce' ) );
}
if( $inscription_iva != 'checkthis' ){
$errors->add( 'digthis', __( 'value should be checkthis', 'woocommerce' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.