Last active
October 29, 2018 10:04
-
-
Save kosmiq/da98611f206de1a9d3dc to your computer and use it in GitHub Desktop.
Add custom checkout fields, validate if payment method cheque is selected. Validate organisational numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Remove if you're not checking the input in org_number | |
include_once( get_stylesheet_directory() . '/pnum.php'); // get from: https://github.com/gurre/php-personnummer/blob/master/pnum.php | |
add_action( 'woocommerce_after_checkout_billing_form', 'cheque_custom_checkout_field' ); | |
function cheque_custom_checkout_field( $checkout ) { | |
echo '<div id="cheque_custom_checkout_field"><h3>' . __('Fakturauppgifter') . '</h3>'; | |
woocommerce_form_field( 'org_number', array( | |
'type' => 'text', | |
'class' => array('org-number-field my-field-class form-row form-row-wide'), | |
'label' => __('Organisationsnummer'), | |
'placeholder' => __('123456-7890'), | |
), $checkout->get_value( 'org_number' )); | |
woocommerce_form_field( 'reference', array( | |
'type' => 'text', | |
'class' => array('reference-field my-field-class form-row form-row-wide'), | |
'label' => __('Referens, beställarid'), | |
'placeholder' => __('Referens'), | |
), $checkout->get_value( 'reference' )); | |
// Is this supposed to be there? Left over from code sent. | |
echo '</div><h3>VAT</h3>'; | |
} | |
add_action('woocommerce_checkout_process', 'cheque_custom_checkout_field_process'); | |
function cheque_custom_checkout_field_process() { | |
// Check if set, if its not set add an error. | |
// Check if org_number is NOT set and payment method is cheque | |
if ( !$_POST['org_number'] && ($_POST['payment_method'] == 'cheque')) { | |
wc_add_notice( __( 'Fyll i <strong><span class="org-number-field-error">Organisationsnummer</span></strong> under fakturauppgifter.' ), 'error' ); | |
} | |
// Check if org_number IS set and payment method is cheque and verify the input in org_number | |
// Remove if you're not checking the input in org_number | |
if ( $_POST['org_number'] && ($_POST['payment_method'] == 'cheque')) { | |
if(Pnum::check($_POST['org_number'], $personal_only = false) == false) | |
wc_add_notice( __( 'Något är fel med <strong><span class="org-number-field-error">Organisationsnummer</span></strong> under fakturauppgifter.' ), 'error' ); | |
} | |
// Check if reference is NOT set and payment method is cheque | |
if ( !$_POST['reference'] && ($_POST['payment_method'] == 'cheque')) { | |
wc_add_notice( __( 'Fyll i <strong><span class="reference-field-error">Referens</span></strong> under fakturauppgifter.' ), 'error' ); | |
} | |
} | |
/** | |
* Update the order meta with field value | |
**/ | |
add_action( 'woocommerce_checkout_update_order_meta', 'cheque_custom_checkout_field_update_order_meta' ); | |
function cheque_custom_checkout_field_update_order_meta( $order_id ) { | |
// If cheque is the selected payment method... Else do nothing here. | |
if( ($_POST['payment_method'] == 'cheque') ) { | |
//check if $_POST has our custom fields | |
// is org_number set? If so save the data. | |
if ( ! empty( $_POST['org_number'] ) ) { | |
//It does: update post meta for this order | |
update_post_meta( $order_id, 'Organisationsnummer', esc_attr( $_POST['org_number'] ) ); | |
} | |
// is reference set? If so save the data. | |
if ( ! empty( $_POST['reference'] ) ) { | |
update_post_meta( $order_id, 'Referens', esc_attr( $_POST['reference'] ) ); | |
} | |
} | |
} | |
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); | |
function my_custom_checkout_field_display_admin_order_meta($order){ | |
// Display whatever the customer entered into org_number and reference and display it when viewing an order. | |
$payment_method = get_post_meta( $order->id, '_payment_method', true ); | |
if( $payment_method == 'cheque' ) { | |
echo '<p><strong>'.__('Organisationsnummer').':</strong> ' . get_post_meta( $order->id, 'Organisationsnummer', true ) . '</p>'; | |
echo '<p><strong>'.__('Referens').':</strong> ' . get_post_meta( $order->id, 'Referens', true ) . '</p>'; | |
} | |
} | |
//add_action( 'wp_head', 'cheque_checkout_fields_jquery' ); //Output the script in <head></head> | |
add_action( 'wp_footer', 'cheque_checkout_fields_jquery', 999 ); // Output the script in <footer></footer> | |
function cheque_checkout_fields_jquery() { | |
// jQuery for showing/hiding fields during checkout if cheque is the selected payment method. | |
// Outputs into [above] but would ideally be placed in the themes main .js-file. | |
echo '<script type="text/javascript"> | |
jQuery(function( $ ){ | |
$( "#order_review" ).on( "click", function() { | |
var payment = $( "#order_review" ).find( "input[name=payment_method]:checked" ); | |
if( payment.is("#payment_method_cheque") ) { | |
$("#cheque_custom_checkout_field").show(400); | |
} else { | |
$("#cheque_custom_checkout_field").hide(400); | |
} | |
}); | |
}); | |
</script>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment