Skip to content

Instantly share code, notes, and snippets.

@ChrisFlannagan
Created June 23, 2016 20:26
Show Gist options
  • Save ChrisFlannagan/c154b93917078c53a41120e25c9f2a0f to your computer and use it in GitHub Desktop.
Save ChrisFlannagan/c154b93917078c53a41120e25c9f2a0f to your computer and use it in GitHub Desktop.
<?php
/**
* Add the SSN field to the checkout if Four Easy Payments is available
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
if( has_deposit() ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('Payment Plan Requirements') . '</h2>';
woocommerce_form_field('ssn_easypay', array(
'type' => 'text',
'label' => __('Payment Plans Require SSN'),
'placeholder' => __('Social Security Number'),
), $checkout->get_value('ssn_easypay'));
echo '</div>';
}
}
/**
* Process the checkout with SSN
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
if( has_deposit() ) {
// Check if set, if its not set add an error.
if ( ! $_POST['ssn_easypay'] || ! preg_match( '#^(\d{3})-(\d{2})-(\d{4})$#', $_POST['ssn_easypay'] ) )
wc_add_notice( __( 'Please enter a valid SSN in the 111-22-3333 format and include the dashes.' ), 'error' );
}
}
function has_deposit() {
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( ! empty( $cart_item['is_deposit'] ) ) {
return true;
}
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment