Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MonteLogic/0ccc71becf686986a37f9fea36c25b83 to your computer and use it in GitHub Desktop.

Select an option

Save MonteLogic/0ccc71becf686986a37f9fea36c25b83 to your computer and use it in GitHub Desktop.

Revisions

  1. @codehooligans codehooligans renamed this gist May 7, 2015. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions gistfile1.php → WC_checkout_field_validation
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    // We are using the WooCommerce Checkout Field Editor plugin in addition to WooCommerce. The Checkout Field Editor
    // does a nice job at providing a UI for manage additional fields. But doesn't provide any method to do validations.
    // So this code helps with that on a very specific implemention where we have a Gender select and a DoB date selector.
    // On the Gender we want to ensure the selected value is 'Male' or 'Female' only. We don't want the
    default option 'Choose Option' to be valid. On the DoB we want to enforce the user is 18 or older.

    add_action( 'woocommerce_after_checkout_validation', 'woo_checkout_additional_field_validation' );
    function woo_checkout_additional_field_validation() {
    if (!defined('WOOCOMMERCE_CHECKOUT')) {
    @@ -7,30 +13,28 @@ function woo_checkout_additional_field_validation() {
    // WooCommerce Checkout Field: DoB
    if (isset($_POST['student_dob'])) {
    if (empty($_POST['student_dob'])) {
    wc_add_notice( __( '<strong>Date of Birth</strong> Please enter a valid date.', 'woocommerce' ), 'error' );
    wc_add_notice( __( '<strong>Date of Birth</strong> Please enter a valid date.', 'voice-verify' ), 'error' );
    } else {
    // Take the selected/entered date and at 18 years to it.
    $dob = esc_attr($_POST['student_dob']);
    $date_dob = date('Y-m-d', strtotime($dob));
    //echo "date_dob[". $date_dob ."]<br />";

    $date_dob = date('Ymd', strtotime($date_dob.' +18 year'));
    //echo "date_dob[". $date_dob ."]<br />";
    $date_dob = date('Ymd', strtotime($dob.' +18 year'));

    // Then take current date
    $date_current = date('Ymd');
    //echo "date_current[". $date_current ."]<br />";

    if ($date_dob > $date_current) {
    wc_add_notice( __( '<strong>Date of Birth</strong> You must be 18 years of age to register for the Adult online drivers education course.', 'woocommerce' ), 'error' );
    wc_add_notice( __( '<strong>Date of Birth</strong> You must be 18 years of age to register for the Adult online drivers education course.', 'voice-verify' ), 'error' );
    }
    }
    }

    // WooCommerce Checkout Field: Gender
    if (isset($_POST['student_gender'])) {
    if (empty($_POST['student_gender'])) {
    wc_add_notice( __( '<strong>Gender</strong> Please select a gender', 'woocommerce' ), 'error' );
    wc_add_notice( __( '<strong>Gender</strong> Please select a gender', 'voice-verify' ), 'error' );
    } else if (($_POST['student_gender'] != 'Male') && ($_POST['student_gender'] != 'Female')) {
    wc_add_notice( __( '<strong>Gender</strong> Please select a valid gender', 'woocommerce' ), 'error' );
    wc_add_notice( __( '<strong>Gender</strong> Please select a valid gender', 'voice-verify' ), 'error' );
    }
    }
    }
    @@ -49,7 +53,7 @@ function process_dob_checkout_field() {
    //console.log('student_dob found');

    //jQuery('#place_order').attr('disabled', 'disabled');
    jQuery('input#student_dob.hasDatepicker').after('<span id="student_dob_error" style="display:none; color: red;">You must be 18 years of age to register</span>');
    jQuery('input#student_dob.hasDatepicker').after('<span id="student_dob_error" style="display:none; color: red;"><?php _e('You must be 18 years of age to register', 'voice-verify'); ?></span>');

    jQuery('input#student_dob.hasDatepicker').datepicker( 'option' , 'onClose', function() {
    //console.log('date picker closed');
  2. @codehooligans codehooligans revised this gist May 7, 2015. 1 changed file with 46 additions and 9 deletions.
    55 changes: 46 additions & 9 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,58 @@
    add_filter( 'wp_footer' , 'woo_add_checkout_field_date_range_limit' );
    function woo_add_checkout_field_date_range_limit() {
    add_action( 'woocommerce_after_checkout_validation', 'woo_checkout_additional_field_validation' );
    function woo_checkout_additional_field_validation() {
    if (!defined('WOOCOMMERCE_CHECKOUT')) {
    return;
    }

    // WooCommerce Checkout Field: DoB
    if (isset($_POST['student_dob'])) {
    if (empty($_POST['student_dob'])) {
    wc_add_notice( __( '<strong>Date of Birth</strong> Please enter a valid date.', 'woocommerce' ), 'error' );
    } else {
    $dob = esc_attr($_POST['student_dob']);
    $date_dob = date('Y-m-d', strtotime($dob));
    //echo "date_dob[". $date_dob ."]<br />";

    $date_dob = date('Ymd', strtotime($date_dob.' +18 year'));
    //echo "date_dob[". $date_dob ."]<br />";

    $date_current = date('Ymd');
    //echo "date_current[". $date_current ."]<br />";

    if ($date_dob > $date_current) {
    wc_add_notice( __( '<strong>Date of Birth</strong> You must be 18 years of age to register for the Adult online drivers education course.', 'woocommerce' ), 'error' );
    }
    }
    }

    // WooCommerce Checkout Field: Gender
    if (isset($_POST['student_gender'])) {
    if (empty($_POST['student_gender'])) {
    wc_add_notice( __( '<strong>Gender</strong> Please select a gender', 'woocommerce' ), 'error' );
    } else if (($_POST['student_gender'] != 'Male') && ($_POST['student_gender'] != 'Female')) {
    wc_add_notice( __( '<strong>Gender</strong> Please select a valid gender', 'woocommerce' ), 'error' );
    }
    }
    }

    add_action( 'wp_footer', 'woo_checkout_field_date_inline_validation' );
    function woo_checkout_field_date_inline_validation() {
    if ( is_checkout() ) {
    ?>
    <script type="text/javascript">
    jQuery( document ).ready( function ( e ) {
    console.log('loaded');
    //console.log('loaded');

    process_dob_checkout_field();
    function process_dob_checkout_field() {
    if (jQuery('input#student_dob.hasDatepicker').length) {
    console.log('student_dob found');
    //console.log('student_dob found');

    jQuery('#place_order').attr('disabled', 'disabled');
    //jQuery('#place_order').attr('disabled', 'disabled');
    jQuery('input#student_dob.hasDatepicker').after('<span id="student_dob_error" style="display:none; color: red;">You must be 18 years of age to register</span>');

    jQuery('input#student_dob.hasDatepicker').datepicker( 'option' , 'onClose', function() {
    console.log('date picker closed');
    //console.log('date picker closed');

    var dob_date_dd = ("0" + jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getDate()).slice(-2);
    var dob_date_mm = ("0" + jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getMonth() + 1).slice(-2);
    @@ -31,13 +68,13 @@ function process_dob_checkout_field() {
    //console.log('today_date[%o]', today_date);

    if (dob_date > today_date) {
    jQuery('#place_order').attr('disabled', 'disabled');
    //jQuery('#place_order').attr('disabled', 'disabled');

    jQuery('span#student_dob_error').show();
    jQuery('#student_dob_field').addClass('woocommerce-invalid');
    jQuery('#student_dob_field').removeClass('woocommerce-validated');
    } else {
    jQuery('#place_order').attr('disabled', false);
    //jQuery('#place_order').attr('disabled', false);

    jQuery('span#student_dob_error').hide();
    jQuery('#student_dob_field').removeClass('woocommerce-invalid');
    @@ -46,7 +83,7 @@ function process_dob_checkout_field() {
    } );

    } else {
    console.log('student_dob NOT found');
    //console.log('student_dob NOT found');
    setTimeout(function() {
    process_dob_checkout_field()

  3. @codehooligans codehooligans revised this gist May 7, 2015. 1 changed file with 1 addition and 8 deletions.
    9 changes: 1 addition & 8 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,4 @@
    /**
    * woo_add_checkout_field_date_range_limit
    *
    * @access public
    * @since 1.0
    * @return void
    * See: http://jqueryui.com/datepicker/#min-max
    */
    add_filter( 'wp_footer' , 'woo_add_checkout_field_date_range_limit' );
    function woo_add_checkout_field_date_range_limit() {
    if ( is_checkout() ) {
    ?>
  4. @codehooligans codehooligans revised this gist May 7, 2015. 1 changed file with 27 additions and 14 deletions.
    41 changes: 27 additions & 14 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -18,27 +18,40 @@ function process_dob_checkout_field() {
    if (jQuery('input#student_dob.hasDatepicker').length) {
    console.log('student_dob found');

    jQuery('input#student_dob.hasDatepicker').after('<span class="error">You must be 18 years of age to register</span>');

    jQuery('#place_order').attr('disabled', 'disabled');
    jQuery('input#student_dob.hasDatepicker').after('<span id="student_dob_error" style="display:none; color: red;">You must be 18 years of age to register</span>');

    jQuery('input#student_dob.hasDatepicker').datepicker( 'option' , 'onClose', function() {
    console.log('date picker closed');

    var dob_date_dd = jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getDate();
    var dob_date_mm = jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getMonth() + 1;
    var dob_date_yy = jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getFullYear() + 18;
    var dob_date = dob_date_yy + "-" + dob_date_mm + "-" + dob_date_dd;
    console.log('dob_date[%o]', dob_date);
    var dob_date_dd = ("0" + jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getDate()).slice(-2);
    var dob_date_mm = ("0" + jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getMonth() + 1).slice(-2);
    var dob_date_yy = jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getFullYear() + 18;
    var dob_date = dob_date_yy +''+ dob_date_mm +''+ dob_date_dd;
    //console.log('dob_date[%o]', dob_date);

    var toDate = new Date();
    var today_date_dd = toDate.getDate();
    var today_date_mm = toDate.getMonth() + 1;
    var today_date_yy = toDate.getFullYear();
    var today_date = today_date_yy + "-" + today_date_mm + "-" + today_date_dd;
    console.log('today_date[%o]', today_date);
    var today_Date = new Date();
    var today_date_dd = ("0" + today_Date.getDate()).slice(-2);
    var today_date_mm = ("0" + today_Date.getMonth() + 1).slice(-2);
    var today_date_yy = today_Date.getFullYear();
    var today_date = today_date_yy +''+ today_date_mm +''+ today_date_dd;
    //console.log('today_date[%o]', today_date);

    if (dob_date > today_date) {
    jQuery('#place_order').attr('disabled', 'disabled');

    jQuery('span#student_dob_error').show();
    jQuery('#student_dob_field').addClass('woocommerce-invalid');
    jQuery('#student_dob_field').removeClass('woocommerce-validated');
    } else {
    jQuery('#place_order').attr('disabled', false);

    jQuery('span#student_dob_error').hide();
    jQuery('#student_dob_field').removeClass('woocommerce-invalid');
    jQuery('#student_dob_field').addClass('woocommerce-validated');
    }
    } );


    } else {
    console.log('student_dob NOT found');
    setTimeout(function() {
  5. @codehooligans codehooligans created this gist May 7, 2015.
    54 changes: 54 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    /**
    * woo_add_checkout_field_date_range_limit
    *
    * @access public
    * @since 1.0
    * @return void
    * See: http://jqueryui.com/datepicker/#min-max
    */
    function woo_add_checkout_field_date_range_limit() {
    if ( is_checkout() ) {
    ?>
    <script type="text/javascript">
    jQuery( document ).ready( function ( e ) {
    console.log('loaded');

    process_dob_checkout_field();
    function process_dob_checkout_field() {
    if (jQuery('input#student_dob.hasDatepicker').length) {
    console.log('student_dob found');

    jQuery('input#student_dob.hasDatepicker').after('<span class="error">You must be 18 years of age to register</span>');

    jQuery('input#student_dob.hasDatepicker').datepicker( 'option' , 'onClose', function() {
    console.log('date picker closed');

    var dob_date_dd = jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getDate();
    var dob_date_mm = jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getMonth() + 1;
    var dob_date_yy = jQuery("input#student_dob.hasDatepicker").datepicker('getDate').getFullYear() + 18;
    var dob_date = dob_date_yy + "-" + dob_date_mm + "-" + dob_date_dd;
    console.log('dob_date[%o]', dob_date);

    var toDate = new Date();
    var today_date_dd = toDate.getDate();
    var today_date_mm = toDate.getMonth() + 1;
    var today_date_yy = toDate.getFullYear();
    var today_date = today_date_yy + "-" + today_date_mm + "-" + today_date_dd;
    console.log('today_date[%o]', today_date);

    } );


    } else {
    console.log('student_dob NOT found');
    setTimeout(function() {
    process_dob_checkout_field()

    }, 500);
    }
    }
    });
    </script>
    <?php
    }
    }