Skip to content

Instantly share code, notes, and snippets.

@navnit-viradiya
Created April 30, 2019 10:43
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 navnit-viradiya/2bb230a64488d9c52ab62e2a22e0c648 to your computer and use it in GitHub Desktop.
Save navnit-viradiya/2bb230a64488d9c52ab62e2a22e0c648 to your computer and use it in GitHub Desktop.
WooCommerce - Add a special field to the checkout, order emails and user/order meta or Load specific custom fields on change country.
<?php
add_action('woocommerce_review_order_before_payment', 'gst_checkout_field');
function gst_checkout_field( $checkout ) {
echo '<div id="gst_checkout_field_sec">';
woocommerce_form_field( 'gst_no', array(
'type' => 'text',
'class' => array('my-field-class orm-row-wide'),
'label' => __('GST No'),
//'placeholder' => __('Enter a number'),
) );
echo '</div>';
/**
* Optional Javascript to limit the field to a country. This one shows for italy only.
**/
?>
<script type="text/javascript">
jQuery(document).ready(function(){
hideShowGstField();
});
jQuery('select#billing_country').live('change', function(){
hideShowGstField();
});
function hideShowGstField(){
var country = jQuery('select#billing_country').val();
var check_countries = new Array(<?php echo '"IN"'; ?>);
if (country && jQuery.inArray( country, check_countries ) >= 0) {
jQuery('#gst_checkout_field_sec').fadeIn();
} else {
jQuery('#gst_checkout_field_sec').fadeOut();
jQuery('#gst_checkout_field_sec input').val('');
}
}
</script>
<?php
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'gst_checkout_field_process');
function gst_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ($_POST['gst_no'])
if (!$_POST['gst_no'])
$woocommerce->add_error( __('Please enter your XXX.') );
}
/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'gst_checkout_field_update_user_meta');
function gst_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['gst_no']) update_user_meta( $user_id, 'gst_no', esc_attr($_POST['gst_no']) );
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'gst_checkout_field_update_order_meta');
function gst_checkout_field_update_order_meta( $order_id ) {
if ($_POST['gst_no']) update_post_meta( $order_id, 'gst_no', esc_attr($_POST['gst_no']));
}
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'gst_checkout_field_order_meta_keys');
function gst_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'gst_no';
return $keys;
}
/**
* Display GST No on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'gst_number_display_func', 10, 1 );
function gst_number_display_func($order){
$order_id = $order->get_id();
$gst_no = get_post_meta( $order_id, 'gst_no', true );
if($gst_no != ''){
echo '<p><strong>GST No: </strong>'.$gst_no.'</p>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment