Skip to content

Instantly share code, notes, and snippets.

@cartimize
Last active January 21, 2022 12:28
Show Gist options
  • Save cartimize/76a0757031ec109ea8a22ef205644bb6 to your computer and use it in GitHub Desktop.
Save cartimize/76a0757031ec109ea8a22ef205644bb6 to your computer and use it in GitHub Desktop.
Here is the example for shipping and billing phone number validation. Add this custom code to your WordPress site
<?php
// Shipping filed validation
add_filter('cartimize_get_shipping_checkout_fields', 'cartimize_modify_shipping_checkout_fields_new', 100, 1);
function cartimize_modify_shipping_checkout_fields_new( $checkout_fields ){
if ( isset($checkout_fields['shipping_phone']) && $checkout_fields['shipping_phone']['required'] == true ) {
$checkout_fields['shipping_phone'] = $checkout_fields['shipping_phone'];
$checkout_fields['shipping_phone']['custom_attributes']['data-parsley-pattern'] = '([0-9]{10})';
}
return $checkout_fields;
}
// Billing filed validation
add_filter('cartimize_get_billing_checkout_fields', 'cartimize_modify_billing_checkout_fields_new', 100, 1);
function cartimize_modify_billing_checkout_fields_new( $checkout_fields ){
if ( isset($checkout_fields['billing_phone']) && $checkout_fields['billing_phone']['required'] == true ) {
$checkout_fields['billing_phone'] = $checkout_fields['billing_phone'];
$checkout_fields['billing_phone']['custom_attributes']['data-parsley-pattern'] = '([0-9]{10})';
}
return $checkout_fields;
}
@cartimize
Copy link
Author

cartimize commented Jan 21, 2022

Use below prefix for South Africa phone number validation

^((?:\+27|27)|0)(\d{2})-?(\d{3})-?(\d{4})$

For example

  1. Shipping section
    $checkout_fields['shipping_phone']['custom_attributes']['data-parsley-pattern'] = '^((?:\+27|27)|0)(\d{2})-?(\d{3})-?(\d{4})$';

  2. Billing section
    $checkout_fields['billing_phone']['custom_attributes']['data-parsley-pattern'] = '^((?:\+27|27)|0)(\d{2})-?(\d{3})-?(\d{4})$';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment