Skip to content

Instantly share code, notes, and snippets.

@mtruitt
Last active December 20, 2023 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mtruitt/9741f611577f4b76dd36bee316af1dee to your computer and use it in GitHub Desktop.
Save mtruitt/9741f611577f4b76dd36bee316af1dee to your computer and use it in GitHub Desktop.
Remove zipcode field and add custom dropdown in its place.
<?php
// Lets remove the postcode
add_filter( 'woocommerce_checkout_fields' , 'custom_wc_checkout_fields' );
function custom_wc_checkout_fields( $fields ) {
unset( $fields['billing']['billing_postcode']);
$fields['billing']['shipping_code'] = array(
'type' => 'select',
'class' => array( 'shipping-code' ),
'label' => __( 'Shipping Code' ),
'required' => true,
'priority' => '80',
'options' => array(
'' => __( 'Select an area ', 'mt' ), // Displays as placeholder and is not selectable
'value for option 1' => __( 'Option 1' , 'mt' ), // Update value for value. Change Option 1 for text to display.
'value for option 2' => __( 'Option 2' , 'mt' ),
'value for option 3' => __( 'Option 3' , 'mt' )
)
);
return $fields;
}
// Add custom field to order meta
add_action( 'woocommerce_checkout_update_order_meta', 'custom_select_checkout_field_update_order_meta' );
function custom_select_checkout_field_update_order_meta( $order_id ) {
if ( $_POST['shipping_code']) {
update_post_meta( $order_id, 'shipping_code', esc_attr($_POST['shipping_code']));
}
}
// Add to admin order area
add_action( 'woocommerce_admin_order_data_after_billing_address', 'add_custom_select_checkout_field_display_admin_order_meta', 10, 1 );
function add_custom_select_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Shipping Code').':</strong><br> ' . get_post_meta( $order->id, 'shipping_code', true ) . '</p>';
}
// Add field value to emails
add_filter('woocommerce_email_order_meta_keys', 'custom_select_order_meta_keys');
function custom_select_order_meta_keys( $keys ) {
$keys['Shipping Code'] = 'shipping_code';
return $keys;
}
// Adds shipping code after Order details on Thank you page and my account > order page
add_action('woocommerce_order_details_after_order_table', 'action_woocommerce_order_details_after_customer_details', 10, 1);
function action_woocommerce_order_details_after_customer_details($order) {
echo '<p><strong>'.__('Shipping Code').':</strong><br> ' . get_post_meta( $order->id, 'shipping_code', true ) . '</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment