Skip to content

Instantly share code, notes, and snippets.

@Xethron
Created June 11, 2013 07:52
Show Gist options
  • Save Xethron/5755129 to your computer and use it in GitHub Desktop.
Save Xethron/5755129 to your computer and use it in GitHub Desktop.
Only display MDS Shipping fields if MDS was selected as the shipping method on Cart Page
<?php
function custom_override_checkout_fields( $fields ) {
// Get global WooCommerce Object
global $woocommerce;
// Get the shipping label
$shipping_label = $woocommerce->cart->shipping_label;
// If the first 3 letters of the shipping lable == MDS
// change fields to the fields required for MDS,
// else, don't change $fields and return input
if (substr($shipping_label, 0, 3)=="MDS"){
$mds = new WC_MDS_Collivery();
$field = $mds->get_field_defaults();
$towns = Array('' => 'Select Town') + $field['towns'];
$cptypes = Array('' => 'Select Premesis Type') + $field['cptypes'];
$billing_data = Array (
'billing_state' => Array (
'type' => 'select',
'label' => 'Town',
'required' => 1,
'class' => Array ('form-row-first', 'update_totals_on_change'),
'options' => $towns,
'selected' => ''
),
'billing_city' => Array (
'type' => 'select',
'label' => 'Suburb',
'required' => 1,
'class' => Array ('form-row-last'),
'options' => Array ('Select town first...')
),
'billing_cptypes' => Array (
'type' => 'select',
'label' => 'Location Type',
'required' => 1,
'class' => Array ('form-row-first', 'update_totals_on_change'),
'options' => $cptypes
),
'billing_company' => Array (
'label' => 'Company Name',
'placeholder' => 'Company (optional)',
'class' => Array ('form-row-last')
),
'billing_building_details' => Array (
'label' => 'Building Details',
'placeholder' => 'Building Details',
'class' => Array ('form-row-wide')
),
'billing_address_1' => Array (
'name' => 'billing-streetno',
'label' => 'Street No.',
'placeholder' => 'Street No.',
'required' => 1,
'class' => Array ('form-row-first')
),
'billing_address_2' => Array (
'name' => 'billing-street',
'label' => 'Street Name',
'placeholder' => 'Street',
'required' => 1,
'class' => Array ('form-row-last')
),
'billing_first_name' => Array (
'label' => 'First Name',
'placeholder' => 'First Name',
'required' => 1,
'class' => Array ('form-row-first')
),
'billing_last_name' => Array (
'label' => 'Last Name',
'placeholder' => 'Last Name',
'required' => 1,
'class' => Array ('form-row-last')
),
'billing_phone' => Array (
'validate' => array('phone'),
'label' => 'Cell Phone',
'placeholder' => 'Phone number',
'required' => 1,
'class' => Array ('form-row-first')
),
'billing_email' => Array (
'validate' => array('email'),
'label' => 'Email Address',
'placeholder' => 'you@yourdomain.co.za',
'required' => 1,
'class' => Array ('form-row-last')
),
);
$shipping_data = Array (
'shipping_state' => Array (
'type' => 'select',
'label' => 'Town',
'required' => 1,
'class' => Array ('form-row-first', 'update_totals_on_change'),
'options' => $towns,
'selected' => ''
),
'shipping_city' => Array (
'type' => 'select',
'label' => 'Suburb',
'required' => 1,
'class' => Array ('form-row-last'),
'options' => Array ('Select town first...')
),
'shipping_cptypes' => Array (
'type' => 'select',
'label' => 'Location Type',
'required' => 1,
'class' => Array ('form-row-first', 'update_totals_on_change'),
'options' => $cptypes
),
'shipping_company' => Array (
'label' => 'Company Name',
'placeholder' => 'Company (optional)',
'class' => Array ('form-row-last')
),
'shipping_building_details' => Array (
'label' => 'Building Details',
'placeholder' => 'Building Details',
'class' => Array ('form-row-wide')
),
'shipping_address_1' => Array (
'name' => 'billing-streetno',
'label' => 'Street No.',
'placeholder' => 'Street No.',
'required' => 1,
'class' => Array ('form-row-first')
),
'shipping_address_2' => Array (
'name' => 'billing-street',
'label' => 'Street Name',
'placeholder' => 'Street',
'required' => 1,
'class' => Array ('form-row-last')
),
'shipping_first_name' => Array (
'label' => 'First Name',
'placeholder' => 'First Name',
'required' => 1,
'class' => Array ('form-row-first')
),
'shipping_last_name' => Array (
'label' => 'Last Name',
'placeholder' => 'Last Name',
'required' => 1,
'class' => Array ('form-row-last')
),
'shipping_phone' => Array (
'validate' => array('phone'),
'label' => 'Cell Phone',
'placeholder' => 'Phone number',
'required' => 1,
'class' => Array ('form-row-first')
),
'shipping_email' => Array (
'validate' => array('email'),
'label' => 'Email Address',
'placeholder' => 'you@yourdomain.co.za',
'required' => 1,
'class' => Array ('form-row-last')
),
);
$fields['billing'] = $billing_data;
$fields['shipping'] = $shipping_data;
}
return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment