How to add an field from user account into checkout page - php

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;
}

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.

Add custom field with maxlength to WooCommerce checkout page

I've added a new field for phone number in WooCommerce checkout form.
Everything is working perfectly except the maxlength = "10". After adding the snippet it shows maxlength = 10 when I inspect element, but the phone field allows me to type n number of characters.
Am I doing anything wrong?
Here is the snippet i used in functions.php
add_action('woocommerce_after_checkout_billing_form', 'custom_checkout_field');
function custom_checkout_field($checkout)
{
woocommerce_form_field('billing_phone_2', array(
'type' => 'number',
'maxlength' => "10",
'id' => 'billing_phone_2',
'class' => array(
'input-text form-row-wide'
) ,
'required' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ),
'label' => __('Phone 2') ,
'placeholder' => __('') ,
) ,
$checkout->get_value('billing_phone_2'));
}
For the type you could use tel, and for the maxlength it is not necessary to put the number between quotes
So you get:
// Display a custom checkout field after billing form
function action_woocommerce_after_checkout_billing_form( $checkout ) {
woocommerce_form_field( 'billing_phone_2', array(
'type' => 'tel',
'maxlength' => 10,
'class' => array( 'form-row-wide' ),
'required' => true,
'label' => __( 'Phone 2', 'woocommerce' ),
'placeholder' => __( 'My placeholder', 'woocommerce' ),
), $checkout->get_value( 'billing_phone_2' ));
}
add_action( 'woocommerce_after_checkout_billing_form', 'action_woocommerce_after_checkout_billing_form', 10, 1 );
Optional: some validation for the custom (required) field
// Custom checkout field validation
function action_woocommerce_checkout_process() {
// Isset
if ( isset( $_POST['billing_phone_2'] ) ) {
$domain = 'woocommerce';
$phone = $_POST['billing_phone_2'];
// Empty
if ( empty ( $phone ) ) {
wc_add_notice( __( 'Please enter a phone number to complete this order', $domain ), 'error' );
}
// Validates a phone number using a regular expression.
if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9_\-\+\/\(\)\.]/', '', $phone ) ) ) ) {
wc_add_notice( __( 'Please enter a valid phone number', $domain ), 'error' );
}
}
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
If type = number, you can use the min and max attributes
// Display a custom checkout field after billing form
function action_woocommerce_after_checkout_billing_form( $checkout ) {
woocommerce_form_field( 'billing_phone_2', array(
'type' => 'number',
'custom_attributes' => array(
'min' => 0,
'max' => 9999999999,
),
'class' => array( 'form-row-wide' ),
'placeholder' => __( 'Phone 2', 'woocommerce' ),
), $checkout->get_value( 'billing_phone_2' ));
}
add_action( 'woocommerce_after_checkout_billing_form', 'action_woocommerce_after_checkout_billing_form', 10, 1 );
Although I wouldn't recommend it for a phone number, there is a reason that the type 'tel' exists, as used in other phonefields in WooCommerce

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 ?

Add new fields at the billing address woocommerce

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;
}

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