add a custom checkout billing field under the last name in WooCommerce - php

In WooCommerce I am adding a custom billing field at the end of the checkout billing fields section, with the code below:
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['billing']['billing_options'] = array(
'label' => __('תאריך לידה', 'woocommerce'), // Add custom field label
'placeholder' => _x('תאריך לידה'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'date', // add field type
'class' => array('my-css') // add class name
);
return $fields;
}
How can I add this field after the first name last name field or after company field?

You need to use the "priority" argument, which will allow you to set your field in the correct location (after the first and last name fields).
Normally "billing first name" has a 10 as priority and "billing last name 20 as priority. Then comes the "billing company" that has 30 as priority… So for your custom billing field use a priority of 25 (in between).
There is a little mistake in your code for the placeholder where you should replace _x() function by __().
Your code is going to be:
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields( $fields )
{
$fields['billing']['billing_options'] = array(
'label' => __('תאריך לידה', 'woocommerce'), // Add custom field label
'placeholder' => __('תאריך לידה', 'woocommerce'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'date', // add field type
'class' => array('my-css'), // add class name
'priority' => 25, // Priority sorting option
);
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
If you want this field after the billing company, you will use a priority of 35 instead.
Related: Reordering checkout fields in WooCommerce 3

Related

PHP : How to change dropdown list to Text ? [Checkout-Page WooCommerce]

WooCommerce on CheckOut-Page , the default of ['billing_state']shows in drop-down list which I want to change it to input[text]. The customer have to key-in the information by themselves.
please help, thanks a lot
this code doesn't work for me. (after the customer key-in the state, it shows the state-code. It should show the state name that the customer-key-in)
add_filter( 'woocommerce_checkout_fields' ,
'y_change_address_input_type', 10, 1 );
function y_change_address_input_type( $fields ) {
$fields['billing']['billing_state']['type'] = 'text';
return $fields;
}
thank you for your comment, currently, I found the solution. I can switch from selection[option] to input[text] on hook woocommerce_default_address_fields, and the code work as I expected. (I don't know why the code will not work on woocommerce_checkout_fields. The code should have work on both woocommerce_default_address_fields and woocommerce_checkout_fields)
This is the work:
add_filter( 'woocommerce_default_address_fields', 'y_edit_state', 40, 1 );
function y_edit_state( $state_fields ) {
$state_fields ['state'] = array(
'label' => 'stae', // Add custom field label
'placeholder' => 'stae', // Add custom field placeholder
'required' => true, // if field is required or not
'class' => array( 'form-row-wide', 'address-field' ), // add class name
'clear' => false, // add clear or not
'type' => 'text', // add field type
'priority' => 80, // Priority sorting option
);
return $state_fields;
}

WooCommerce searchable drop down list

so I've implemented a billing form with some custom fields , my drop down is :
$fields['billing_complex_name'] = array(
'label' => __('Complex Name', 'woocommerce'), // Add custom field label
'placeholder' => _x('E.g Raslouw Gardens', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'select',// add field type
'options' => array(
'' => 'Please select',
'ansaarestate'=> 'Ansaar Estate',
'bangladeshheights'=> 'Bangladesh Heights',
'celticmanor'=> 'Celtic Manor',
'chantelplace'=> 'Chantel Place',
...
'class' => array('my-css'),
'priority' => 51,//
);
And then using jQuery I implement the following for changes:
jQuery(document).ready(function(){
// Your code in here
jQuery(document).on('input','#billing_complex_name', function() {
myFunc();
})
function myFunc() {
// your function code
//var phone_num = jQuery('#billing_phone').val(<?php $phone_number ?>);
var complex_name = jQuery('#billing_complex_name').val();
var suburb = jQuery('#billing_suburb').val();
if (complex_name == 'eldogleneast') {
jQuery("#billing_suburb").val('ELD');
jQuery('#billing_postcode').val('0157');
jQuery("#billing_complex_address").val('');
jQuery("#billing_postcode").prop("readonly", false);
And of course depending on the selection of the complex conditions change such as the postcode or suburb etc.
My question now is:
I need to make the drop down list searchable and have an Other selection , if Other is selected I need a text input for the user to enter in and that will be the selected value (All jQuery then will just not make any changes to the postcode or anything)
Used select2.js library to implement it

WooCommerce custom child products to variable products

I have some variable products (different 3-4 variations) with different prices. I want to add some simple products as child products (show as optional checkbox in front). I don't know how to manage that for adding to the cart.
For example let's assume I have a cellphone as main product and when user added it to it's cart it should see 3 sub product (child product) :
Handsfree (in three type of color)
Glass guard (in two type of color )
cell phone shield case ( just black)
as you can understand from above example I wanna to:
add all of this items as one product (includes handsfree, glass guard , cellphone case and cellphone itself ) and calculate it's price .I've read Add a checkbox on single product pages that adds an additional cost in Woocommerce and after that I have codes below to adding options in product edit page.
//custom product tab to woocommerce edit
function sm_wccpd_custom_product_tabs($tabs)
{
$tabs['sm_wcpa_extra_products'] = array(
'label' => __('Samanik Extra Products', 'sm-wcpa'),
'target' => 'sm_wc_extra_products_options',
'class' => array('show_if_variable'),
);
return $tabs;
}
add_filter('woocommerce_product_data_tabs', 'sm_wccpd_custom_product_tabs');
and here is tab content
function extra_products_options_product_tab_content()
{
global $post;
// Note the 'id' attribute needs to match the 'target' parameter set above
?>
<div id='sm_wc_extra_products_options' class='panel woocommerce_options_panel'><?php
?>
<div class='options_group'><?php
woocommerce_wp_checkbox(
[
'id' => '_has_extra_products',
'label' => __('has extra products', 'sm-wcpa'),
]);
woocommerce_wp_select(
[
'class' => 'multiselect attribute_values wc-enhanced-select',
'custom_attributes' => ['multiple' => 'multiple', 'style' => 'width:100% !importan;'],
'id' => '_sm_wcpa_product[]',
'label' => __('Extra Products to be add here', 'sm-wcpa'),
'value' => array_values(json_decode(get_post_meta($post->ID, '_sm_wcpa_product'), true)),
'options' => sm_wcpa_get_products_as_options(),//this is function that I pass Products as array like [product_id => product name]
]);
?></div>
</div><?php
}
// add_filter('woocommerce_product_data_tabs', 'extra_products_options_product_tab_content'); // WC 2.5 and below
add_filter('woocommerce_product_data_panels', 'extra_products_options_product_tab_content'); // WC 2.6 and up
also I can save the data and I think they are not necessary here.
now what I need to do is how to manage this in front. I mean show child products as checkbox, add price to parent price if checked, what about changing variables(those have different prices).
thanks for your patient to read my question.

Customizing checkout Postcode field into a custom drop-down menu

I am building a WooCommerce based store. I have a list of postcodes, each one has a different shipping cost attached through Shipping Zones (some provide free shipping, some have a flat rate).
When the customer goes to the checkout page, he needs to type his postcode number in the input field. Depending on postcode, an order preview will show different shipping total (free or flat rate).
Here's how the input field looks like in class-wc-countries.php:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
),
);
However, what I want to do is to turn this field into a drop-down menu, so the customer could just select his postcode option rather than type it.
I managed to make it drop-down, but whenever I choose any option it doesn't seem to change shipping total as it would with input field.
Here's what I did:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
'type' => 'select',
'options' => array(
'opt1' => "001122", "112200", "334400")
),
);
But this don't work.
Am I missing something?
How do I make these drop-down options change shipping total?
Thanks
This will answer very partially to your question, and just show you the way to customize checkout fields.
Overriding core files is not really something to do, as you will loose everithing each time Woocommerce is going to be updated and is not recommended.
To override checkout fields in a clean way, first you need to use a custom function hooked in one of that 2 filter hooks:
woocommerce_default_address_fields (when customizing billing and shipping address default fields)
woocommerce_checkout_fields (when customizing billing or shipping address fields and also others fields).
Related official documentation: Customizing checkout fields using actions and filters
So here I have chose the first hook, and I have corrected your post codes array. You will get that:
Here is that functional and tested code:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_postcode_field' );
function custom_override_default_postcode_field( $address_fields ) {
// Your postcodes array
$postcode_array = array(
'opt1' => "001122",
'opt2' => "112200",
'opt3' => "334400"
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
As selecting a post-code is a live front-end event, you will need to use a complex javascript/ajax script with some remote php function to achieve what you want to do, and this is a real development... It also depends on your settings and is complex to handle as there is already some woocommerce ajax scripts operating in that checkout page.

woocommerce additional billing field

I'm trying to add an additional field in woocommerce billing part. I want "title" field to be displayed before the "name" field.
I have tried this:
// Add a new checkout field
function custom_filter_checkout_fields($fields){
$fields['billing_title_field'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_filter_checkout_fields' );
and then
function order_fields($fields) {
$order = array(
"billing_title_field",
"billing_first_name",
"billing_last_name",
"billing_email",
"billing_phone",
"billing_country",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_company"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
But this returns me no field I have created in the first step. I'ts clear that I'm missing a crusial part of the process, but I cannot figure out what exactly. Search on internet was no success (the methods provided there are adding fields before or after billing fields and I need to add it inside the group of billing fields).
Any help is appreciated! Thanks in advance!
add this plugin
http://phppoet.com/docs/checkout-fields/
and add your field you want
So I guess I found the answer. Actually in the first piece of code I have missed an important thing. That should look like that:
function custom_filter_checkout_fields($fields){
$fields['billing']['billing_title_field'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_filter_checkout_fields');
and then the second piece of code to set the created field to the right place.

Categories