Im adding a custom address field in checkout with this code:
add_filter( 'woocommerce_checkout_fields' , 'checkout_address_details_fields' );
// Our hooked in function – $fields is passed via the filter!
function checkout_address_details_fields( $fields ) {
$fields['shipping']['shipping_address_details'] = array(
'label' => __('Añade más detalles a tu dirección', 'woocommerce'),
'placeholder' => _x('Bloque X Apartemento XXX (Opcional)', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['billing_address_details'] = array(
'label' => __('Añade más detalles a tu dirección', 'woocommerce'),
'placeholder' => _x('Bloque X Apartamento XXX (Opcional)', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
It is adding the field correctly, however, the field is added at the end below phone field, I need to add this custom field in the 2nd place, below the Street Address field.
You need to use the "priority" argument to set the correct location of your checkout fields like:
add_filter( 'woocommerce_checkout_fields' , 'checkout_address_details_fields' );
function checkout_address_details_fields( $fields ) {
$fields['shipping']['shipping_address_details'] = array(
'label' => __('Añade más detalles a tu dirección', 'woocommerce'),
'placeholder' => _x('Bloque X Apartemento XXX (Opcional)', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'priority' => 55, // <===== Here
);
$fields['billing']['billing_address_details'] = array(
'label' => __('Añade más detalles a tu dirección', 'woocommerce'),
'placeholder' => _x('Bloque X Apartamento XXX (Opcional)', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'priority' => 55, // <===== Here
);
return $fields;
}
It should work.
Related
I've setup 4 fields in the woocommerce checkout form which are displaying correctly in the edit order page and customer panel, but I'm not able to display correctly the checkbox as a yes or no (results in a 1 or 0) and can't get those custom fields in the new order admin email.
I've tried different codes to add the fields in the new order email, but all resulted in PHP errors or just didn't show the fields.
The code I've used is:
// Campi extra checkout
add_filter( 'woocommerce_billing_fields' , 'add_billing_field_extra' );
function add_billing_field_extra( $fields ) {
$new_order_fields = array();
foreach($fields as $key => $value){
if($key == 'billing_country') {
$new_order_fields['billing_fattura'] = array(
'type' => 'checkbox',
'label' => __('Vuoi la fattura?', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
);
$new_order_fields['billing_piva'] = array(
'label' => __('Partita Iva', 'woocommerce'),
'placeholder' => _x('Partita iva', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'show' => true
);
$new_order_fields['billing_cf'] = array(
'label' => __('Codice Fiscale', 'woocommerce'),
'placeholder' => _x('Codice Fiscale', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'show' => true
);
$new_order_fields['billing_sdi'] = array(
'label' => __('SDI', 'woocommerce'),
'placeholder' => _x('SDI', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'show' => true
);
}
$new_order_fields[$key] = $value;
}
return $new_order_fields;
}
// Aggiungere il campo Piva AMMINISTRAZIONE BACKEND
add_filter( 'woocommerce_admin_billing_fields' , 'add_admin_field_cfpiva' );
function add_admin_field_cfpiva( $fields ) {
$fields['fattura'] = array(
'label' => __('Fattura', 'woocommerce'),
'show' => true
);
$fields['piva'] = array(
'label' => __('P.IVA', 'woocommerce'),
'show' => true
);
$fields['cf'] = array(
'label' => __('Codice Fiscale', 'woocommerce'),
'show' => true
);
$fields['sdi'] = array(
'label' => __('SDI', 'woocommerce'),
'show' => true
);
return $fields;
}
function blindo_mostra_nascondi_campi_woocommerce() {
wc_enqueue_js( "
jQuery('input#billing_fattura').change(function(){
if (! this.checked) {
// HIDE IF NOT CHECKED
jQuery('#billing_piva_field').fadeOut();
jQuery('#billing_piva_field input').val('');
jQuery('#billing_cf_field').fadeOut();
jQuery('#billing_cf_field input').val('');
jQuery('#billing_sdi_field').fadeOut();
jQuery('#billing_sdi_field input').val('');
} else {
// SHOW IF CHECKED
jQuery('#billing_piva_field').fadeIn();
jQuery('#billing_cf_field').fadeIn();
jQuery('#billing_sdi_field').fadeIn();
}
}).change();
");
}
I am customizing the WooCommerce checkout page fields. I want to add a heading (text) in between the fields. I have reordered the fields like this
add_filter('woocommerce_checkout_fields', 'ac_checkout_reorder_fields');
function ac_checkout_reorder_fields($fields) {
$order = array(
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_email",
"billing_phone",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_country"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
Then I added a new heading like this
add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_heading' );
function add_custom_heading( $checkout ) {
echo '<div id="add_custom_heading"><h2>' . __('Custom Heading Here') . '</h2></div>' ;
}
Now the fields are re-arranged and the custom heading is showing. But I want the heading showing just below the name & company fields. With my code, the field is being added below. How I an reposition this in my desired place.
PS: I also tried to add customize the sections of the entire field with this hook woocommerce_checkout_fields adding placeholder and removing the labels. Here are the codes but this also does not help me solve the issue.
function add_wc_custom_fields($fields) {
global $woocommerce;
$countries_obj = new WC_Countries();
$countries = $countries_obj->__get('countries');
$fields['billing']['billing_first_name'] = array(
'label' => '',
'placeholder' => _x('First Name*', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array( 'form-row-first' ),
);
$fields['billing']['billing_last_name'] = array(
'label' => '',
'placeholder' => _x('last Name*', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array( 'form-row-last' ),
);
$fields['billing']['billing_company'] = array(
'label' => '',
'placeholder' => _x('Company Name', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('checkout-billing-company')
);
$fields['billing']['billing_address_1'] = array(
'label' => '',
'placeholder' => _x('Address(Line 1)*', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('checkout-billing-addressL1')
);
$fields['billing']['billing_address_2'] = array(
'label' => '',
'placeholder' => _x('Address(Line 2)*', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('checkout-billing-addressL2')
);
$fields['billing']['billing_email'] = array(
'label' => '',
'placeholder' => _x('Email Address*', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-first')
);
$fields['billing']['billing_phone'] = array(
'label' => '',
'placeholder' => _x('Phone Number', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-last')
);
$fields['billing']['billing_city'] = array(
'label' => '',
'placeholder' => _x('Town/City', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-first')
);
$fields['billing']['billing_state'] = array(
'label' => '',
'placeholder' => _x('State/County', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-first')
);
$fields['billing']['billing_postcode'] = array(
'label' => '',
'placeholder' => _x('Zip/Postcode', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-first')
);
$fields['billing']['billing_country'] = array(
'label' => '',
'type' => 'select',
'placeholder' => _x('Country', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-last'),
'options' => $countries
);
return $fields;
}
add_filter('woocommerce_checkout_fields', 'add_wc_custom_fields');
we can use the filter 'woocommerce_form_field_' . $type... where $type is the type of the input... in our case billing_company is of type text... this filter returns the html of the field, in our case billing field, billing_company.. the filter has 4 arguments being passed, these are $field, $key, $args, $value... we just need two of these...
add_action( 'woocommerce_form_field_text','reigel_custom_heading', 10, 2 );
function reigel_custom_heading( $field, $key ){
// will only execute if the field is billing_company and we are on the checkout page...
if ( is_checkout() && ( $key == 'billing_company') ) {
$field .= '<p class="form-row form-row-wide">Custom Heading</p>';
}
return $field;
}
Important: If we don’t format it as a paragraph with the class form-row it will be put to the top of all billing fields by Woocommerce (reasons unknown to me). This shows us that this as “Hack” and maybe your goals are achieved better differently.
Instead of hooking into an existing woocommerce_form_field_<field_type> filter such as woocommerce_form_field_text, I prefer to use the woocommerce_form_field_<field_type> filter to create a new field type. This allows us to add HTML output for an additional field type (and therefore we can use HTML output for a heading instead) and we can use this new heading "field type" when modifying checkout fields with the woocommerce_checkout_fields filter.
// Add field type to WooCommerce form field
add_filter( 'woocommerce_form_field_heading','sc_woocommerce_form_field_heading', 10, 4 );
function sc_woocommerce_form_field_heading($field, $key, $args, $value) {
$output = '<h3 class="form-row form-row-wide">'.__( $args['label'], 'woocommerce' ).'</h3>';
echo $output;
}
// Modify checkout fields
add_filter( 'woocommerce_checkout_fields','custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_heading_name'] = array(
'type' => 'heading',
'label' => 'Heading here',
);
Using the above method the solution to the original question would be:
add_filter('woocommerce_checkout_fields', 'ac_checkout_reorder_fields');
function ac_checkout_reorder_fields($fields) {
$fields['billing']['billing_heading_name'] = array(
'type' => 'heading',
'label' => 'Heading here',
);
$order = array(
"billing_first_name",
"billing_last_name",
"billing_heading_name",
"billing_company",
"billing_email",
"billing_phone",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_country"
);
foreach($order as $field) {
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
add_filter('woocommerce_form_field', 'addHeadingsInBetweenFormFields', 10, 4);
function addHeadingsInBetweenFormFields($field, $key, $args, $value = null) {
if (is_checkout() & $key === 'billing_first_name') {
$a = '<p class="form-row form-row-wide">Shipping</p>';
return $a . $field;
}
return $field;
}
I am little depressed for a few days now, trying to find a solution for this, so any help would be much appreciated.
In myfunctions.php in Wordpress, I added the following:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
unset($address_fields['postcode']);
$address_fields['company']['required'] = false;
$address_fields['chamber_of_commerce_no'] = array(
'label' => __('Chamber of Commerce No', 'woocommerce'),
'placeholder' => _x('Chamber of Commerce No', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$address_fields['fiscal_code'] = array(
'label' => __('Fiscal Code', 'woocommerce'),
'placeholder' => _x('For romanian companies', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$address_fields['vat_code'] = array(
'label' => __('VAT Code', 'woocommerce'),
'placeholder' => _x('For foreign companies', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$address_fields['bank_account_no'] = array(
'label' => __('Bank Account No', 'woocommerce'),
'placeholder' => _x('Bank Account No', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$address_fields['bank'] = array(
'label' => __('Bank', 'woocommerce'),
'placeholder' => _x('Bank', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $address_fields;
}
After this, I tried adding the fields to get_formatted_billing_address function:
public function get_formatted_billing_address() {
if ( ! $this->formatted_billing_address ) {
// Formatted Addresses
$address = apply_filters( 'woocommerce_order_formatted_billing_address', array(
'company' => $this->billing_company,
'address_1' => $this->billing_address_1,
'address_2' => $this->billing_address_2,
'city' => $this->billing_city,
'country' => $this->billing_country,
'bank_account_no' => $this->billing_bank_account_no,
'bank' => $this->billing_bank,
'chamber_of_commerce_no'=> $this->billing_chamber_of_commerce_no,
'fiscal_code' => $this->billing_fiscal_code,
'vat_code' => $this->billing_vat_code
), $this );
$this->formatted_billing_address = WC()->countries->get_formatted_address( $address );
}
return $this->formatted_billing_address;
}
It did not work.
Then I tried adding a filter:
add_filter ('woocommerce_order_formatted_billing_address', 'custom_override_formatted_billing_address');
function custom_override_formatted_billing_address () {
$address = array(
'company' => $this->billing_company,
'address_1' => $this->billing_address_1,
'address_2' => $this->billing_address_2,
'city' => $this->billing_city,
'state' => $this->billing_state,
'country' => $this->billing_country,
'bank_account_no' => $this->billing_bank_account_no,
'bank' => $this->billing_bank,
'chamber_of_commerce_no'=> $this->billing_chamber_of_commerce_no,
'fiscal_code' => $this->billing_fiscal_code,
'vat_code' => $this->billing_vat_code
);
return $address;
}
Still, it did not work.
How can do I make this work?
Thanks!
You need to do this:
add_filter('woocommerce_localisation_address_formats', 'your_func')
I adda ed the below code to function.php page and it shows the select box at checkout page fine. But how can i get the value of selected item at the thank you page.
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['point_side'] = array(
'label' => __(
'<strong>Select Where Your Points Should Go</strong>',
'woocommerce'
),
'placeholder' => _x(
'custom_field', 'placeholder',
'woocommerce'
),
'required' => true,
'clear' => false,
'type' => 'select',
'class' => array('form-row-wide'),
'options' => array(
'default1' => __('Defult1', 'woocommerce'),
'ls' => __('Left Side', 'woocommerce'),
'rs' => __('Right Side', 'woocommerce')
)
);
return $fields;
}
Any helps appreciated.
I have managed to add custom checkout city dropdown field by following method
$fields['shipping']['shipping_city'] = array(
'label' => __('City', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => true,
'type' => 'select',
'class' => array('own-css-name'),
'options' => array(
'New_York_City' => __('New York City', 'woocommerce' ),
'Chicago' => __('Chicago', 'woocommerce' ),
'Dallas' => __('Dallas', 'woocommerce' )
)
);
I want to get the option values from city taxonomy so I have tried the following method but it doesn't seems to work (http://i.stack.imgur.com/dasIm.jpg)
$args = array(
'child_of' => 0,
'type' => 'product',
'taxonomy' => 'city',
'hide_empty' => 0
);
$categories = get_categories( $args );
foreach ($categories as $category) {
$cityArray[] = "'".$category->slug."' => __('".$category->name."', 'woocommerce' )";
}
$fields['shipping']['shipping_city2'] = array(
'label' => __('City', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => true,
'type' => 'select',
'class' => array('own-css-name'),
'options' => array(implode( ', ', $cityArray ) )
);
Try this .
Just replace the
For Loop with
foreach ($categories as $category) {
$cityArray[$category->slug] = $category->name;
}
Set option like this
'options' => array_values($cityArray)
Have tested the same and the results where as follows
Let me know if this worked for you.