Skip to content

Instantly share code, notes, and snippets.

@Longkt
Last active July 2, 2018 09:22
Show Gist options
  • Save Longkt/2d4106babcf9989f2d6bb23254d6e72c to your computer and use it in GitHub Desktop.
Save Longkt/2d4106babcf9989f2d6bb23254d6e72c to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WC Apartment Number
* Plugin URI: https://github.com/longkt/wc-apartment-number
* Description: This plugin add new field "Apartment Number" for the billing address.
* Author: longnguyen
* Author URI: https://profiles.wordpress.org/longnguyen
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
new WC_Apartment_Number();
class WC_Apartment_Number {
public $wdm_address_fields;
public function __construct() {
$this->wdm_address_fields = array(
'first_name',
'last_name',
'company',
'apartment_number',
'address_1',
'address_2',
'city',
'postcode',
'country',
'state',
);
// Hook to billing fields
add_filter( 'woocommerce_default_address_fields', array( $this, 'add_field_apartment_number' ) );
// Hook to checkout page
add_action( 'woocommerce_checkout_process', array( $this, 'check_apartment_number' ) );
//add_filter( 'woocommerce_formatted_address_replacements', array( $this, 'format_address_replacements' ) );
add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'wdm_update_formatted_billing_address' ) );
//add_filter( 'woocommerce_get_order_address', array( $this, 'order_address' ) );
//add_filter( 'woocommerce_localisation_address_formats', array( $this, 'reformat_address' ) );
}
// Add Apartment Number field
public function add_field_apartment_number( $fields ) {
$temp_fields = array();
$fields['apartment_number'] = array(
'label' => __( 'Apartment Number', 'woocommerce' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'autocomplete' => 'given-name',
'priority' => 45,
);
foreach( $this->wdm_address_fields as $fky ){
$temp_fields[$fky] = $fields[$fky];
}
$fields = $temp_fields;
return $fields;
}
// Check user input is number, if not a number don't process to checkout
public function check_apartment_number() {
if( isset( $_POST['billing_apartment_number'] ) && ! is_numeric( $_POST['billing_apartment_number'] ) ) {
wc_add_notice( __('Your Apartment Number must be number'), 'error' );
}
}
public function format_address_replacements( $address, $args ) {
$address['{apartment_number}'] = $args['apartment_number'];
return $address;
}
public function wdm_update_formatted_billing_address( $address, $obj ){
if( is_array( $this->wdm_address_fields ) ){
foreach( $this->wdm_address_fields as $waf ){
$address[$waf] = $obj->{'billing_'.$waf};
}
}
return $address;
}
public function order_address( $address, $type, $order ) {
$number = $type . '_apartment_number';
// WooCommerce 3.0 or later.
if ( method_exists( $order, 'get_meta' ) ) {
$address[$type]['apartment_number'] = $order->get_meta( '_' . $number );
} else {
$address[$type]['apartment_number'] = $order->$number;
}
return $address;
}
public function reformat_address( $address ) {
$address = array(
'default' => "{name}\n{company}\n{apartment_number}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}"
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment