• Greetings!

    I have created a new fields in woocommerce which are respectively

    Travel Date From:
    Travel Date To:

    This is the code in order to get the date:

    <?php
    echo get_post_meta( $post->ID, '_traveldates', true );
    ?>

    or

    <?php
    echo get_post_meta( get_the_ID(), '_traveldates', true );
    ?>

    I would like to know if ever there is a way to validate the date that it should only be within those from and to dates. For example, if the travel dates are june 2016 – december 2016, it would show an error

    “Please Pick within June 2016 – December 2016”

    Thanks, Help would be really appreciated.

    https://wordpress.org/plugins/wc-fields-factory/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Saravana Kumar K

    (@mycholan)

    Good option, I will try to include this as configurable with my next release.

    mean time you can validate from your functions.php using the following snippet.

    function validate_wccpf( $passed, $pid = null ) {
    	if( isset( $_REQUEST[ "your_date_field_name" ] ) ) {
    
    		$dateBegin = strtotime( "07/12/1985" );
    		$dateEnd = strtotime( "26/02/2016" );
    
    		$givendate = strtotime( $_REQUEST[ "your_date_field_name" ] );
    
    		if( $givendate < $dateBegin && $givendate > $dateEnd ) {
    			return false;
    		}
    
    	}
    	return true;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'validate_wccpf', 100, 2 );

    haven’t tested though, let me know whether it works.

    Thread Starter kingslayer21

    (@kingslayer21)

    problems I have:

    1.) It does not work
    2.) “your_date_field_name” – What do you mean by this? is it the slug or the name of the field group? my field group name is “Date” and slug is “via”
    3.) regarding the $dateBegin & $dateEnd, I think it should be the field names right? I have From and To on my Date Field groups, so should it be:
    “$dateBegin=strtotime(“syntax of how to get the from date on the WC fields group”)”

    I think that’s it

    will wait for your reply.

    Thanks

    Thread Starter kingslayer21

    (@kingslayer21)

    Hi Saravana, any news about my problem above ? Thanks

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Date Picker within the Dates only’ is closed to new replies.