Skip to content

Instantly share code, notes, and snippets.

@georgejipa
Created May 11, 2016 19:15
Show Gist options
  • Save georgejipa/fd7bfa462aba567a186fb803e31e8efc to your computer and use it in GitHub Desktop.
Save georgejipa/fd7bfa462aba567a186fb803e31e8efc to your computer and use it in GitHub Desktop.
billing new fields
<?php
/*
Plugin Name: WooCommerce New Fields
Plugin URI: https://georgejipa.com/
Description: Add new billing fields
Version: 1.0
Author: George Jipa
Author URI: http://georgejipa.com
*/
add_action('admin_enqueue_scripts', 'load_billing_fields_assets');
function load_billing_fields_assets() {
$screen = get_current_screen();
if (isset($screen->id) && $screen->id == 'shop_order') {
wp_enqueue_script('billing_fields_js', plugin_dir_url(__FILE__) . 'script.js', array('jquery'), null, true);
}
}
function add_woocommerce_admin_billing_fields($billing_fields) {
$billing_fields['bloc'] = array(
'label' => __('Bloc', 'woocommerce')
);
$billing_fields['scara'] = array(
'label' => __('Scara.', 'woocommerce')
);
$billing_fields['apartament'] = array(
'label' => __('Apartament', 'woocommerce')
);
$billing_fields['etaj'] = array(
'label' => __('Etaj', 'woocommerce')
);
$billing_fields['interfon'] = array(
'label' => __('Interfon', 'woocommerce')
);
return $billing_fields;
}
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_found_customer_details($customer_data, $user_id, $type_to_load) {
if ($type_to_load == 'billing') {
$customer_data[$type_to_load . '_bloc'] = get_user_meta($user_id, $type_to_load . '_bloc', true);
$customer_data[$type_to_load . '_scara'] = get_user_meta($user_id, $type_to_load . '_scara', true);
$customer_data[$type_to_load . '_apartament'] = get_user_meta($user_id, $type_to_load . '_apartament', true);
$customer_data[$type_to_load . '_etaj'] = get_user_meta($user_id, $type_to_load . '_etaj', true);
$customer_data[$type_to_load . '_interfon'] = get_user_meta($user_id, $type_to_load . '_interfon', true);
}
return $customer_data;
}
add_filter('woocommerce_found_customer_details', 'add_woocommerce_found_customer_details', 10, 3);
function add_woocommerce_billing_fields($billing_fields) {
$billing_fields['billing_bloc'] = array(
'type' => 'text',
'label' => __('Bloc'),
'placeholder' => _x('Block', 'placeholder'),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
$billing_fields['billing_scara'] = array(
'type' => 'text',
'label' => __('Scara'),
'placeholder' => _x('Scara', 'placeholder'),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
$billing_fields['billing_apartament'] = array(
'type' => 'text',
'label' => __('Apartament'),
'placeholder' => _x('Apartament', 'placeholder'),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
$billing_fields['billing_etaj'] = array(
'type' => 'text',
'label' => __('Etaj'),
'placeholder' => _x('Etaj', 'placeholder'),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
$billing_fields['billing_interfon'] = array(
'type' => 'text',
'label' => __('Interfon'),
'placeholder' => _x('Interfon', 'placeholder'),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
return $billing_fields;
}
add_filter('woocommerce_billing_fields', 'add_woocommerce_billing_fields');
function add_woocommerce_customer_meta_fields($billing_fields) {
if (isset($billing_fields['billing']['fields'])) {
$billing_fields['billing']['fields']['billing_bloc'] = array(
'label' => __('Bloc', 'woocommerce'),
'description' => ''
);
$billing_fields['billing']['fields']['billing_scara'] = array(
'label' => __('Scara', 'woocommerce'),
'description' => ''
);
$billing_fields['billing']['fields']['billing_apartament'] = array(
'label' => __('Apartament', 'woocommerce'),
'description' => ''
);
$billing_fields['billing']['fields']['billing_etaj'] = array(
'label' => __('Etaj', 'woocommerce'),
'description' => ''
);
$billing_fields['billing']['fields']['billing_intefon'] = array(
'label' => __('Interfon', 'woocommerce'),
'description' => ''
);
}
return $billing_fields;
}
add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
function add_woocommerce_order_fields($address, $order) {
$address['billing_bloc'] = $order->billing_bloc;
$address['billing_scara'] = $order->billing_scara;
$address['billing_apartament'] = $order->billing_apartament;
$address['billing_etaj'] = $order->billing_etaj;
$address['billing_intefon'] = $order->billing_intefon;
return $address;
}
add_filter('woocommerce_order_formatted_billing_address', 'add_woocommerce_order_fields', 10, 2);
function add_woocommerce_formatted_address_replacements($replace, $args) {
$replace['{billing_bloc}'] = !empty($args['billing_bloc']) ? 'Bl ' . $args['billing_bloc'] : '';
$replace['{billing_scara}'] = !empty($args['billing_scara']) ? 'Sc ' . $args['billing_scara'] : '';
$replace['{billing_apartament}'] = !empty($args['billing_apartament']) ? 'Ap ' . $args['billing_apartament'] : '';
$replace['{billing_etaj}'] = !empty($args['billing_etaj']) ? 'Et ' . $args['billing_etaj'] : '';
$replace['{billing_intefon}'] = !empty($args['billing_intefon']) ? 'Interfon ' . $args['billing_intefon'] : '';
return $replace;
}
add_filter('woocommerce_formatted_address_replacements', 'add_woocommerce_formatted_address_replacements', 10, 2);
function add_woocommerce_localisation_address_formats($formats) {
$formats['default'] = $formats['default'] . "\n{billing_bloc}\n{billing_scara}\n{billing_apartament}\n{billing_etaj}\n{billing_intefon}";
return $formats;
}
add_filter('woocommerce_localisation_address_formats', 'add_woocommerce_localisation_address_formats', 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment