Skip to content

Instantly share code, notes, and snippets.

@grola
Created August 6, 2019 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grola/a7b9cba54263755c621daee13a31a0f8 to your computer and use it in GitHub Desktop.
Save grola/a7b9cba54263755c621daee13a31a0f8 to your computer and use it in GitHub Desktop.
Polish postcode custom validation for Flexible Checkout Field
<?php
/**
* Validate polish postcode.
*
* @param string $field_label .
* @param string $value .
*/
function wpdesk_is_polish_postcode( $field_label, $value ) {
if ( ! ( (bool) preg_match( '/^([0-9]{2})-([0-9]{3})$/', $value ) ) ) {
wc_add_notice( sprintf( '%s nie jest poprawnym kodem pocztowym!', '<strong>' . $field_label . '</strong>' ), 'error' );
}
}
/**
* Add custom FCF validation.
*
* @param array $custom_validation .
*
* @return array
*/
function wpdesk_polish_postcode_custom_validation( $custom_validation ) {
$custom_validation['is_polish_postcode'] = array(
'label' => 'Polski kod pocztowy',
'callback' => 'wpdesk_is_polish_postcode',
);
return $custom_validation;
}
add_filter( 'flexible_checkout_fields_custom_validation', 'wpdesk_polish_postcode_custom_validation' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment