Add new fields at the billing address woocommerce - php

I want to edit my billing address at my website, where i need to add and delete some others in my account page, which code shall I edit?
Thank you in advanced

Can you please check below code you can add new custom field example.
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing']['billing_options'] = array(
'label' => __('Custom Field', 'woocommerce'), // Add custom field label
'placeholder' => _x('Custom Field', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('own-css-name') // add class name
);
return $fields;
}

To delete an existing field, country for example:
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
function custom_override_billing_fields( $fields ) {
unset($fields['billing_country']);
return $fields;
}

maybe the best way use woocommerce_default_address_fields - is it ?
for example as
#5447232
add_filter( 'woocommerce_default_address_fields', 'add_MyNewOption_address' );
function add_MyNewOption_address( $fields ) {
$fields['MyNewOption'] = array(
'label' => __('Custom Field', 'woocommerce'), // Add custom field label
'placeholder' => _x('Custom Field', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('own-css-name') // add class name
);
return $fields;
}

Related

How to reorder additional fields on checkout page in WooCommerce

I cant figure out how to reorder the additional fields, on the checkout page in WooCommerce.
I have added one extra field to the WooCommerce additional information section. I would like to show the time field first then the order notes below it.
This is the code that I am using:
add_filter( 'woocommerce-additional-fields', 'custom_order_fields', 20, 1 );
function custom_order_fields( $fields ) {
$fields['order_comments']['priority'] = 80;
$fields['woocommerce-delivery-time-field']['priority'] = 70;
return $fields;
}
However, this does not have the desired result. Can someone tell me what I'm doing wrong?
If you want to show your custom field first, and then the order notes.
You can either use:
// Add 'delivery time' field before 'order comments'
function filter_woocommerce_checkout_fields( $fields ) {
// Get 'order comments' field
$order_comments = $fields['order']['order_comments'];
// Unset 'order comments' field
unset( $fields['order']['order_comments'] );
// Add 'delivery time' field
$fields['order']['delivery_time'] = array(
'label' => __( 'Delivery time', 'woocommerce' ),
'required' => true,
'type' => 'text',
'class' => array( 'form-row-wide' ),
);
// Add 'order comments' field
$fields['order']['order_comments'] = $order_comments;
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'filter_woocommerce_checkout_fields', 10, 1 );
OR use the woocommerce_before_order_notes action hook
function action_woocommerce_before_order_notes( $checkout ) {
// Add field
woocommerce_form_field( 'delivery_time', array(
'type' => 'text',
'class' => array( 'form-row form-row-wide' ),
'label' => __( 'Delivery time', 'woocommerce' ),
'required' => true,
), $checkout->get_value( 'delivery_time' ) );
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
You would need to add the field to the WooCommerce Custom Field first before you set the priority likeso.
add_action('woocommerce_checkout_fields', 'add_woocommerce_additional_fields');
// Function to add field
function add_woocommerce_additional_fields( $fields ) {
$fields['order']['delivery_time'] = array(
'type' => 'text',
'label' => __('Delivery time', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
// You can set your priority here
// Just higher than it a bit
$fields['order']['order_comments']['priority'] = 80;
$fields['order']['delivery_time']['priority'] = 70;
return $fields;
}
You can check here for more information on ordering of fields in Woocommerce.

How to add an field from user account into checkout page

I want to add a field from user account into checkout fields in woocommerce. I have already a field "EU VAT Number" with its values for each user in account. I want to display this field and its value after billing details on checkout page.
I created a filter, but it displayw only the Field label with no value.
Below is my code
// Display a custom field on checkout and on My account > edit
billing address
add_filter( 'woocommerce_billing_fields' ,
'adding_billing_eu_vat_number', 20, 1 );
$vat = get_field('billing_eu_vat_number',
'user_'.get_current_user_id());
if( $vat = get_field( 'billing_eu_vat_number',
'user_'.get_current_user_id() ) ) { echo "<h3
style='margin:0;font-weight: 600;'>" . 'EU VAT NUMBER: ' .
$vat . '</h3>' ; }
function adding_billing_eu_vat_number ( $fields ) {
$fields['eu_vat_number'] = array(
'label' => __('EU VAT Number', 'woocommerce'),
'placeholder' => _x($vat, 'placeholder', 'woocommerce'),
'class' => array('form-row-wide'),
'required' => true,
'clear' => true,
);
return $fields;
}
I need each customer not to insert each time he order the eu vat number and to be already inserted.
Thank you in advance!
You need to give it a default value,
Try this
add_filter( 'woocommerce_billing_fields' , 'adding_billing_eu_vat_number', 20, 1 );
function adding_billing_eu_vat_number ( $fields ) {
$eu_vat_number = get_field('billing_eu_vat_number', 'user_'.get_current_user_id());
$fields['eu_vat_number'] = array(
'label' => __('EU VAT Number', 'woocommerce'),
'placeholder' => _x($vat, 'placeholder', 'woocommerce'),
'class' => array('form-row-wide'),
'required' => true,
'clear' => true,
'default' => $eu_vat_number;
);
return $fields;
}

woocommerce add custom fields data to $package

I am creating a custom field in billing section in woocommerce cart via this code:
add_filter( 'woocommerce_default_address_fields', 'fts_cdek_fields_address' );
function fts_cdek_fields_address($fields){
$fields['cdek_city_id'] = array(
'type' => 'text',
'label' => __('Shipping city id. Technically needed.', $WCFCT),
'placeholder' => '',
'class' => array( 'wc_fts_cdek_hide','update_totals_on_change','form-row-wide','address-field' ),
'required' => false,
'clear' => false,
'label_class' => array( 'wc_fts_cdek_hide' )
);
return $fields;
}
function fts_cdek_fields_error(){
// Check if thechnical field is clear => user didn't enter the city or entered it wrong! (Without cdek api and autocomplete)
if(!isset($_POST['billing_cdek_city_id'])){
wc_add_notice( __( 'Please reenter city in shipping area using autocomplete.' ), 'error' );
}
}
add_action('woocommerce_checkout_update_order_meta', 'fts_cdek_fields_save');
function fts_cdek_fields_save( $order_id ){
if( !empty( $_POST['billing_cdek_city_id'] ) ){
update_post_meta( $order_id, 'CDEK city ID', $_POST['billing_cdek_city_id'] );
}
}
And I want to add my fields value to $package variable in woocommerce custom shipping ( calculate_shipping). How can I add my data there ?

WooCommerce field labels not shown

For some reason, my WooCommerce isn't showing labels nor placeholders for custom fields. No matter whether I'm adding the fields manually through functions.php (code below) or using plugins like 'WooCommerce Mailchimp', the fields shows up alright but there's no label next to it.
What might be causing this type of error?
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
Change the key from shipping_phone to something else.
It seems like some key names are reserved even if they are not shown in the checkout fields list: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

How can i make custom field value required ( compulsory ) in woocommerce product page when adding product

I have added custom text box field in my add product page in woocommerce. Now i want to make it required (compulsory). I tried it by passing argument "required"=>true. but it is not working. Please see below code
woocommerce_wp_text_input(
array(
'id' => 'special_price',
'label' => __( 'Wholesaler Price *', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'required' => 'true',
'description' => __( 'Enter wholesaler price here.', 'woocommerce' )
)
);
but it is not making textbox compulsory. Please can anyone tell how can i do this?
For the required HTML5 attribute and other custom attributes woocommerce_wp_text_input() function has custom_attributes option.
woocommerce_wp_text_input(
array(
'id' => 'special_price',
'label' => __( 'Wholesaler Price *', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'custom_attributes' => array( 'required' => 'required' ),
'description' => __( 'Enter wholesaler price here.', 'woocommerce' )
)
);
You can modify the following code as per your need.
// Validate when adding to cart
add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_to_cart_validation_custom', 10, 3 );
/ validation
function woocommerce_add_to_cart_validation_custom($passed, $product_id, $qty){
global $woocommerce;
$option = ''; // your custom field's name
if( isset($_POST[sanitize_title($option)]) && $_POST[sanitize_title($option)] == '' )
$passed = false;
if (!$passed)
$woocommerce->add_error( sprintf( __('"%s" is a required field.', 'woocommerce'), $option) );
return $passed;
}
For even more options while adding a product in the cart you may find How to add a custom text box value to cart session array in Woocommerce my this answer helpful.

Categories