Woocommerce: populating a billing address field from functions.php - php

I have added a custom field to the Woocommerce create new order page (admin) with this code:
function rt_woocommerce_admin_billing_fields( $fields ) {
$fields['billing_email'] = array(
'label' => __( 'Email' ),
'show' => true,
'class' => 'short',
);
return $fields;
}
add_filter('woocommerce_admin_billing_fields', 'rt_woocommerce_admin_billing_fields');
My question is how do I then populate this new field when the page loads? I want to populate it from a URL token using
$email = get_query_var('email');
But how do I insert that value into the new field so it is visible to the admin while creating the order?

To give a value to the field, change the $field to:
$email = get_query_var('email');
$fields['billing_email'] = array(
'label' => __( 'Email' ),
'value' => $email,
'show' => true,
'class' => 'short',
);

Related

How to display custom address fields value to frontend in woocommerce My-account -> Addresses?

I have added a custom field called "Nick Name" to billing and shipping address form.
add_filter( 'woocommerce_default_address_fields', 'add_nickname_field' );
function add_nickname_field( $fields ) {
$fields['nickname_field'] = array(
'label' => 'Nick Name',
'required' => false,
'class' => array( 'form-row-wide', 'my-custom-class' ),
'priority' => 10,
'placeholder' => '',
);
return $fields;
}
// This will show the nick name field on edit-user screen
add_filter( 'woocommerce_customer_meta_fields', 'admin_address_field' );
function admin_address_field( $admin_fields ) {
$admin_fields['billing']['fields']['billing_nickname_field'] = array(
'label' => 'Nick Name',
'description' => '',
);
$admin_fields['shipping']['fields']['shipping_nickname_field'] = array(
'label' => 'Nick Name',
'description' => '',
);
return $admin_fields;
}
Everything is working fine data is storing in the database and the stored value is also visible to the backend Edit user page. But, It is neither showing in the front end nor prefilled to the address form.
How should I make it work so that the stored value will be displayed at the frontend?
Any help will much be appreciated!
EDIT: I am also using saved addresses for woocommerce plugin that allows users to save multiple addresses.

Check value from Gravity Forms API

I am creating a custom Gravity Forms add-on and it appears to work so far. The settings are showing and saving as expected.
Here's what I have:
public function plugin_settings_fields() {
return array(
array(
'title' => esc_html__( 'Animal Types', 'animaltypes' ),
'fields' => array(
array(
'name' => 'gravity_forms_animal_types',
'type' => 'checkbox',
'label' => esc_html__( 'Animal Types', 'animaltypes' ),
'choices' => array(
array(
'label' => esc_html__( 'Cat', 'animaltypes' ),
'name' => 'option_cat',
'default_value' => 0,
),
array(
'label' => esc_html__( 'Dogs', 'animaltypes' ),
'name' => 'option_dog',
'default_value' => 0,
)
)
),
)
)
);
}
But what I can't figure out is how to check, for example, if option_cat has been set so that I can then run a custom function if it is.
So essentially (and I know the below code is not correct) something like this:
if(option_cat == true) {
my_cat_function();
}
In gravity forms when you create a new addon you provide a slug for that addon. Gravity forms save that settings with the help of that slug.
So if you want to get that settings you can use below code.
$settings = get_option('gravityformsaddon_slugname__settings');
$option = rgar( $settings, 'gravity_forms_animal_types' );
In options you can get selection of your settings, and if you want to one selection at a time you must use radio button instead of checkbox.
It was quite simple after all.
$options = get_option('gravityformsaddon_animaltypes_settings');
$cat = $options['option_cat'];
if($cat) {
my_cat_function();
}

Dynamically add key value pair to options property of woocommerce checkout fields filter

I am adding some custom fields to woocommerce checkout using woocommerce_checkout_fields filter. One of those fields is a select dropdown. This is my code for the fields.
// Add a new checkout field
function ds_filter_checkout_fields($fields){
$suburb = ds_get_delivery_suburbs();
$postcodes = ds_get_delivery_postcodes();
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
),
'select_field' => array(
'type' => 'select',
'options' => array('key' => 'value'),
'required' => true,
'label' => __( 'Another field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ds_filter_checkout_fields' );
If you check the select_field code there is a options property and it takes key and value pair ... I want to insert dynamic key and dynamic value to options property ... In the code I am getting the dynamic key from $postcodes and dynamic value from $suburb and when I try to insert it like this 'options' => array($postcodes => $suburb), I get this warning Warning: Illegal offset type ... I have tried couple of other methods but they didn't work so I turned to you guys ... I appreciate your help ... looking forward to your responses.
NOTE: I have googled this but haven't found any answers so that is why I turned to Stackoverflow for help.
If you want it to be dynamic you have to first set and fill the array and then use it, so it would be
// Add a new checkout field
function ds_filter_checkout_fields($fields){
$suburb = ds_get_delivery_suburbs();
$postcodes = ds_get_delivery_postcodes();
$ma_options[$postcodes] = $suburb;
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
),
'select_field' => array(
'type' => 'select',
'options' => $ma_options[$postcodes],
'required' => true,
'label' => __( 'Another field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ds_filter_checkout_fields' );
If it doesn't work then share var_dump($ma_options);
--- UPDATE
That could happen because of index not existing or passing something which isn't an string as the key. You should cast your type to string if desired efect, or use stdClass object.

Wordpress Woocommerce get value from custom shipping field (AJAX)

So I have an ecommerce using woocommerce and I use custom shipping for track shipping fee.
And I already add new input data (select). like you can see on below picture:
// Hook in
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_city'] = array(
'type' => 'select',
'label' => __('Kota / Kabupaten', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kota / Kabupaten'
)
);
$fields['shipping']['shipping_city'] = array(
'type' => 'select',
'label' => __('Kota / Kabupaten', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kota / Kabupaten'
)
);
$fields['billing']['billing_subdistrict'] = array(
'type' => 'select',
'label' => __('Kecamatan', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kecamatan'
)
);
$fields['shipping']['shipping_subdistrict'] = array(
'type' => 'select',
'label' => __('Kecamatan', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kecamatan'
)
);
return $fields;
}
Woocommerce default data had address_1,address_2,country,state,city but I need one more data called subdistrict. I don't need to save that data (subdistrict). But I need to use that value as parameter for track shipping fee.
I already create new class-custom-shipping-delivery.php.
and I already make sure that function work perfectly because I already try to set $subdistrict data manually.
//custom-shipping.php
$province = $package['destination']['state'];
$city = $package['destination']['city'];
$subdistrict= 'something';
//How to get the data from custom field (ajax)
//because I need to see the shipping fee result before Checkout (and update it to add rate)
$destination_code = $this->getDestinationCode($province,$city,$subdistrict);
$ongkir = $this->cek_ongkir($origin,$destination_code,$weight);
//print_r();
// send the final rate to the user.
$this->add_rate(array(
'id' => $this->id,
'label' => $this->title,
'cost' => $ongkir
));
Summary:
How to get Value from Subdistrict Input type select (on checkout page)?
Sorry I just edit from another person work so I'm not understand that code at all. But I think they forgot to get that value because they just hardcode it and I'm a newbie on wordpress so I don't know how to pass data on checkout form.
After searching the answer for a while then I get the answer.
So for Summary it:
Above answer use session to get the data from shipping custom field (ajax).
So when AJAX run. I send value of 'subdistrict' field to function and save it to session.
function ajax_process($subdistrict){
//some code
session_start();
$_SESSION['subdistrict'] = $subdistrict;
}
then for get the session data from other function I run this code :
#session_start();
$subdistrict = $_SESSION['subdistrict'];
Here is a plugin called Checkout Field Editor for Woocommerce.
https://docs.woocommerce.com/document/checkout-field-editor/
Looks like that plugin costs $49 for a single site license.
The other option is to code it yourself. Here is a tutorial.
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

Showing a description under input field on WooCommerce Checkout Page

Scenario - We're working on a particular WooCommerce site that is meant for Charities only. So on the Checkout page, we'd like to change "Company Name" to "Charity Name"
We've manage to to that with the following code. -
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing_company'] = array(
'label' => __('Charity Name', 'woothemes'),
'placeholder' => __('Charity Name', 'woothemes'),
'required' => true,
'class' => array('billing-company'),
);
return $fields;
}
This changes the field name alright, however we require a description to show up below the Label that reads - "Please enter the full name of your Charity"
We tried adding the line
'description' => __( 'Please enter the full name of your Charity', 'woothemes' )
However, it doesn't seem to work or show up on the page or even in the code.
Could someone please guide me with the same.
Thank you in advance.
This isn't well documented, but these are the default fields for the woocommerce_form_field() function, which is where the array is passed, so you can determine the possible keys. It is found in includes/wc-template-functions.php:
$defaults = array(
'type' => 'text',
'label' => '',
'placeholder' => '',
'maxlength' => false,
'required' => false,
'class' => array(),
'label_class' => array(),
'input_class' => array(),
'return' => false,
'options' => array(),
'custom_attributes' => array(),
'validate' => array(),
'default' => '',
);
There isn't a way in this method to append your own HTML, but you could hack it in using the custom_attributes (save your text description) and input_class (tell jQuery to use this to append to the DOM) options.
Add CSS class and custom attribute to hold the values for jQuery
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing_company'] = array(
'label' => __('Charity Name', 'woothemes'),
'placeholder' => __('Charity Name', 'woothemes'),
'required' => true,
'class' => array('billing-company'),
'custom_attributes' = array( 'item-description'=>'YOUR DESCRIPTION' ),
'input_class' = array( 'append-description' ),
);
return $fields;
}
Use jQuery to use your CSS class as a selector and grab the description
jQuery('.append-description').each(function(){
var item = jQuery(this);
var description = item.attr('item-description');
item.parent().append('<div>'+description+'</div>');
});
Please note the above code is untested and likely doesn't work, use as an example for your implementation.
Description is now available. You can add one and it will show as a span="description" underneath.

Categories