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.
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();
");
}
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.
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'm trying to add a bunch of custom fields at the end of the checkout form that collects additional data we'd need to process an order.
This is what I have:
add_action( 'woocommerce_after_order_notes', 'student_info_fields' );
function student_info_fields( $checkout ) {
echo '<div id="student_info"><span>The following information below will be used to create an account.</span>';
//This adds a student_name field
woocommerce_form_field( 'student_name', array(
'type' => 'text',
'class' => array('form-row-wide'),
'label' => __('Student Name'),
'placeholder' => __('eg: "John Smith", "Johnny", etc'),
'required' => 'true',
), $checkout->get_value( 'student_name' ));
/* I have two other text fields here that follow the same syntax as student_name */
//This adds a student_gender field
woocommerce_form_field( 'student_gender', array(
'type' => 'select',
'label' => __('Gender', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => 'true',
'options' => array(
'male' => __('Male', 'woocommerce' ),
'female' => __('Female', 'woocommerce' )
),
), $checkout->get_value( 'student_gender' ));
echo '</div>';
}
The text fields all seem to work, but when I add the student_gender field, my page breaks (all white screen). I'm a little skeeved out by calling the options array within the student_gender array, as well as calling the $checkout ->get_value... line after I declare each field, but I simply don't know what to do.
Any direction you can give me would be so helpful to cracking this nut. Thanks for sticking with it!
I looked at it and figured out what I was screwing up. That is to say, it pointed me toward my label declaration, which I don't think was using the proper syntax.
I changed my code to:
//This adds a student_gender field
woocommerce_form_field( 'student_gender', array(
'type' => 'select',
'label' => __('Student Gender'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => 'true',
'options' => array(
'male' => __('Male', 'woocommerce' ),
'female' => __('Female', 'woocommerce' )
),
), $checkout->get_value( 'student_grade' ));
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.