Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hdytsgt/b71eb642e0c4bb1d6b99bbc1cc68a48a to your computer and use it in GitHub Desktop.
Save hdytsgt/b71eb642e0c4bb1d6b99bbc1cc68a48a to your computer and use it in GitHub Desktop.
Add apartment number field in checkout billing adress.
<?php
/**
* Plugin Name: WooCommerce Checkout Apartment No. Field
* Plugin URI: https://github.com/hdytsgt
* Description: Add apartment number field in checkout billing adress.
* Version: 1.0.0
* Author: Hidayat Sagita
* Author URI: https://github.com/hdytsgt
* Developer: Hidayat Sagita
* Developer URI: https://github.com/hdytsgt
* Text Domain: woocommerce-checkout-apartmentno-field
* Domain Path: /i18n/languages/
*
* Copyright: 2018 Hidayat Sagita.
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Quit if accessed directly
*/
if( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Make sure WooCommerce is active
*/
if( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
return;
}
/**
* Display apartment field in checkout billing address
*
* @param Array $fields
* @return Array
*/
function wc_apartment_show_field( $fields ) {
// Declare the new field
$fields[ 'billing' ][ 'billing_apartment_no' ] = [
'type' => 'text',
'placeholder' => _x( 'Apartment number (optional)', 'placeholder', 'woocommerce' ),
'required' => false
];
// Reorder fields so it makes sense
$fields_order = [
'billing_first_name', 'billing_last_name', 'billing_company', 'billing_email',
'billing_phone', 'billing_country', 'billing_address_1', 'billing_address_2',
'billing_apartment_no', 'billing_city', 'billing_postcode', 'billing_state'
];
foreach( $fields_order as $field ) {
$ordered_fields[ 'billing' ][ $field ] = $fields[ 'billing' ][ $field ];
}
// Merge with default fields
$fixed_fields = array_merge( $fields, $ordered_fields );
return $fixed_fields;
}
/**
* New field validation.
* Make sure only allow number.
*
* @param Array $data
* @param Object $errors
* @return Void
*/
function wc_apartment_validate_field( $data, $errors ) {
if( $data[ 'billing_apartment_no' ] && !is_numeric( $data[ 'billing_apartment_no' ] ) ) {
$errors->add( 'billing_apartment_no', _x( 'Please make sure <strong>Apartment number</strong> is only number', 'placeholder', 'woocommerce' ), 'error' );
}
}
/**
* Make sure new field is registered for visibility in any location : WP Admin, Account and Thank You page
*
* @param Array $fields
* @param Object $order
* @return Array
*/
function wc_order_formatted_billing_address( $fields, $order ) {
$fields[ 'billing_apartment_no' ] = get_post_meta( $order->get_id(), '_billing_apartment_no', true );
return $fields;
}
/**
* Replace new field tag with real value
*
* @param Array $tags
* @param Array $address
* @return Array
*/
function wc_formatted_address_replacements( $tags, $address ) {
$tags[ '{billing_apartment_no}' ] = isset( $address[ 'billing_apartment_no' ] ) ? $address[ 'billing_apartment_no' ] : '';
return $tags;
}
/**
* Make sure localized address format to include new field
*
* @param Array $formats
* @return Array
*/
function wc_localisation_address_formats( $formats ) {
$formats[ 'default' ] = "{name}\n{company}\n{address_1}\n{address_2}\n{billing_apartment_no}\n{city}\n{state}\n{postcode}\n{country}";
return $formats;
}
/**
* Initialize this plugin after ensuring WooCommerce is loaded.
*
* @return Void
*/
function wc_checkout_apartmentno_field_init() {
// Register field in checkout fields hook
add_action( 'woocommerce_checkout_fields', 'wc_apartment_show_field' );
// Validate new field
add_action( 'woocommerce_after_checkout_validation', 'wc_apartment_validate_field', 10, 2 );
// Make sure the new field visible in any location : Backend, Tracking and Thank You page
add_filter( 'woocommerce_order_formatted_billing_address', 'wc_order_formatted_billing_address', 10, 2 );
add_filter( 'woocommerce_formatted_address_replacements', 'wc_formatted_address_replacements', 10, 2 );
add_filter( 'woocommerce_localisation_address_formats', 'wc_localisation_address_formats', 10, 2 );
}
add_action( 'plugins_loaded', 'wc_checkout_apartmentno_field_init', 11 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment