Skip to content
Browse files

Extended Support for custom Billing Fields

* added new checkout field type: checkbox
* changed checkout data saving to parse all $billing fields
* changed admin order data display and saving to get all billing fields
(even custom)
  • Loading branch information...
1 parent 46144a2 commit fd4b82d4ae47fb3a0dae5ce67dd9bd974b5077e0 @arminrosu committed Feb 21, 2012
Showing with 65 additions and 53 deletions.
  1. +28 −11 admin/write-panels/order-data-save.php
  2. +17 −27 admin/write-panels/order-data.php
  3. +20 −15 classes/jigoshop_checkout.class.php
View
39 admin/write-panels/order-data-save.php
@@ -34,17 +34,6 @@ function jigoshop_process_shop_order_meta($post_id, $post)
// Add/Replace data to array
$order_fields = array(
- 'billing_first_name',
- 'billing_last_name',
- 'billing_company',
- 'billing_address_1',
- 'billing_address_2',
- 'billing_city',
- 'billing_postcode',
- 'billing_country',
- 'billing_state',
- 'billing_email',
- 'billing_phone',
'shipping_first_name',
'shipping_last_name',
'shipping_company',
@@ -66,6 +55,34 @@ function jigoshop_process_shop_order_meta($post_id, $post)
'order_shipping_tax',
'order_total'
);
+
+ // get billing fields (and check for custom fields)
+ if ( has_filter('jigoshop_billing_fields') ) :
+ // get all billing fields
+ $billing_fields = jigoshop_checkout::instance()->billing_fields;
+
+ // append to $order_fields
+ foreach ($billing_fields as $field) :
+ $order_fields[] = str_replace('-', '_', $field['name']);
+ endforeach;
+ else :
+ // use defaults
+ $billing_fields = array(
+ 'billing_first_name',
+ 'billing_last_name',
+ 'billing_company',
+ 'billing_address_1',
+ 'billing_address_2',
+ 'billing_city',
+ 'billing_postcode',
+ 'billing_country',
+ 'billing_state',
+ 'billing_email',
+ 'billing_phone'
+ );
+
+ $order_fields = array_merge($billing_fields, $order_fields);
+ endif;
//run stripslashes on all valid fields
foreach ($order_fields as $field_name) {
View
44 admin/write-panels/order-data.php
@@ -100,33 +100,23 @@ function jigoshop_order_data_meta_box($post) {
<div id="order_customer_billing_data" class="panel jigoshop_options_panel">
<?php
//display billing fieds and values
-
- $billing_fields = array(
- 'first_name' => __('First Name', 'jigoshop'),
- 'last_name' => __('Last Name', 'jigoshop'),
- 'company' => __('Company', 'jigoshop'),
- 'address_1' => __('Address 1', 'jigoshop'),
- 'address_2' => __('Address 2', 'jigoshop'),
- 'city' => __('City', 'jigoshop'),
- 'postcode' => __('Postcode', 'jigoshop'),
- 'country' => __('Country', 'jigoshop'),
- 'state' => __('State/County', 'jigoshop'),
- 'email' => __('Email Address', 'jigoshop'),
- 'phone' => __('Tel', 'jigoshop'),
- );
-
- foreach($billing_fields as $field_id => $field_desc) {
- $field_id = 'billing_' . $field_id;
- $field_value = '';
-
- if(isset($data[$field_id])) {
- $field_value = $data[$field_id];
- }
-
- echo '<p class="form-field"><label for="' . esc_attr( $field_id ) . '">'.$field_desc.':</label>
- <input type="text" name="'.esc_attr($field_id).'" id="'.esc_attr($field_id).'" value="'.esc_attr($field_value).'" /></p>';
- }
-
+
+ // get all fields (including custom ones)
+ // this is expensive!!!
+ $billing_fields = jigoshop_checkout::instance()->billing_fields;
+
+ // parse fields
+ foreach ($billing_fields as $field) :
+ $field_id = str_replace('-', '_', $field['name']);
+
+ if(isset($data[$field_id])) :
+ $field_value = $data[$field_id];
+ endif;
+
+ echo '<p class="form-field"><label for="' . esc_attr( $field_id ) . '">'.$field['label'].':</label>
+ <input type="text" name="'.esc_attr($field_id).'" id="'.esc_attr($field_id).'" value="'.esc_attr($field_value).'" /></p>';
+
+ endforeach;
?>
</div>
View
35 classes/jigoshop_checkout.class.php
@@ -38,7 +38,7 @@ protected function __construct () {
array( 'name'=>'billing-first_name', 'label' => __('First Name', 'jigoshop'), 'placeholder' => __('First Name', 'jigoshop'), 'required' => true, 'class' => array('form-row-first') ),
array( 'name'=>'billing-last_name', 'label' => __('Last Name', 'jigoshop'), 'placeholder' => __('Last Name', 'jigoshop'), 'required' => true, 'class' => array('form-row-last') ),
array( 'name'=>'billing-company', 'label' => __('Company', 'jigoshop'), 'placeholder' => __('Company', 'jigoshop') ),
- array( 'name'=>'billing-address', 'label' => __('Address', 'jigoshop'), 'placeholder' => __('Address 1', 'jigoshop'), 'required' => true, 'class' => array('form-row-first') ),
+ array( 'name'=>'billing-address-1', 'label' => __('Address', 'jigoshop'), 'placeholder' => __('Address 1', 'jigoshop'), 'required' => true, 'class' => array('form-row-first') ),
array( 'name'=>'billing-address-2', 'label' => __('Address 2', 'jigoshop'), 'placeholder' => __('Address 2', 'jigoshop'), 'class' => array('form-row-last'), 'label_class' => array('hidden') ),
array( 'name'=>'billing-city', 'label' => __('City', 'jigoshop'), 'placeholder' => __('City', 'jigoshop'), 'required' => true, 'class' => array('form-row-first') ),
array( 'type'=>'postcode', 'validate' => 'postcode', 'format' => 'postcode', 'name'=>'billing-postcode', 'label' => __('Postcode', 'jigoshop'), 'placeholder' => __('Postcode', 'jigoshop'), 'required' => true, 'class' => array('form-row-last') ),
@@ -239,6 +239,12 @@ function checkout_form_field( $args ) {
<input type="'.$args['type'].'" class="input-text" name="'.esc_attr($args['name']).'" id="'.esc_attr($args['name']).'" placeholder="'.$args['placeholder'].'" value="' . esc_attr( $current_pc ) . '" />
</p>'.$after;
break;
+ case "checkbox" :
+ $field = '<p class="form-row '.implode(' ', $args['class']).'">
+ <label for="' . esc_attr( $args['name'] ) . '" class="'.implode(' ', $args['label_class']).'">'.$args['label'].$required.'</label>
+ <input type="checkbox" class="input-text' . esc_attr( $input_required ) . '" name="'.esc_attr($args['name']).'" id="'.esc_attr($args['name']).'" '.($args['checked'] ? ' checked="checked"' : '' ).' />
+ </p>'.$after;
+ break;
case "textarea" :
$field = '<p class="form-row '.implode(' ', $args['class']).'">
@@ -456,7 +462,7 @@ function process_checkout() {
$shipping_first_name = $this->posted['billing-first_name'];
$shipping_last_name = $this->posted['billing-last_name'];
$shipping_company = $this->posted['billing-company'];
- $shipping_address_1 = $this->posted['billing-address'];
+ $shipping_address_1 = $this->posted['billing-address-1'];
$shipping_address_2 = $this->posted['billing-address-2'];
$shipping_city = $this->posted['billing-city'];
$shipping_state = $this->posted['billing-state'];
@@ -483,7 +489,7 @@ function process_checkout() {
update_user_meta( $user_id, 'billing-last_name', $this->posted['billing-last_name'] );
update_user_meta( $user_id, 'billing-company', $this->posted['billing-company'] );
update_user_meta( $user_id, 'billing-email', $this->posted['billing-email'] );
- update_user_meta( $user_id, 'billing-address', $this->posted['billing-address'] );
+ update_user_meta( $user_id, 'billing-address-1', $this->posted['billing-address-1'] );
update_user_meta( $user_id, 'billing-address-2', $this->posted['billing-address-2'] );
update_user_meta( $user_id, 'billing-city', $this->posted['billing-city'] );
update_user_meta( $user_id, 'billing-postcode', $this->posted['billing-postcode'] );
@@ -505,7 +511,7 @@ function process_checkout() {
update_user_meta( $user_id, 'shipping-first_name', $this->posted['billing-first_name'] );
update_user_meta( $user_id, 'shipping-last_name', $this->posted['billing-last_name'] );
update_user_meta( $user_id, 'shipping-company', $this->posted['billing-company'] );
- update_user_meta( $user_id, 'shipping-address', $this->posted['billing-address'] );
+ update_user_meta( $user_id, 'shipping-address', $this->posted['billing-address-1'] );
update_user_meta( $user_id, 'shipping-address-2', $this->posted['billing-address-2'] );
update_user_meta( $user_id, 'shipping-city', $this->posted['billing-city'] );
update_user_meta( $user_id, 'shipping-postcode', $this->posted['billing-postcode'] );
@@ -529,17 +535,16 @@ function process_checkout() {
// Order meta data
$data = array();
- $data['billing_first_name'] = $this->posted['billing-first_name'];
- $data['billing_last_name'] = $this->posted['billing-last_name'];
- $data['billing_company'] = $this->posted['billing-company'];
- $data['billing_address_1'] = $this->posted['billing-address'];
- $data['billing_address_2'] = $this->posted['billing-address-2'];
- $data['billing_city'] = $this->posted['billing-city'];
- $data['billing_postcode'] = $this->posted['billing-postcode'];
- $data['billing_country'] = $this->posted['billing-country'];
- $data['billing_state'] = $this->posted['billing-state'];
- $data['billing_email'] = $this->posted['billing-email'];
- $data['billing_phone'] = $this->posted['billing-phone'];
+
+ // add billig fields
+ // looping through $billing_fields so that custom fields will be saved too
+ foreach ($this->billing_fields as $field) :
+ // make name nice
+ $data_name = str_replace('-', '_', $field['name']);
+
+ $data[$data_name] = $this->posted[$field['name']];
+ endforeach;
+
$data['shipping_first_name'] = $shipping_first_name;
$data['shipping_last_name'] = $shipping_last_name;
$data['shipping_company'] = $shipping_company;

0 comments on commit fd4b82d

Please sign in to comment.
Something went wrong with that request. Please try again.