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/
Related
I've created some custom checkout fields in Woocommerce. This works so far and fields are visible in backend as soon as order is placed.
However i would like to show all of these fields only if user has selected local pickup in the checkout. And furthermore i would like to hide certain fields of the newly created ones if the radiobutton has a specific option or vice versa. So for instance if isPickupDateSure==1 show pickupOptionSelection else show order_pickup_datetime_local
add_action('woocommerce_review_order_before_payment', 'pickupOptions_custom_checkout_field');
function pickupOptions_custom_checkout_field($checkout) {
// all fields only to be shown if user has selected local pickup
// radio button to select if pickup is sure or not
woocommerce_form_field( 'isPickupDateSure', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array('1' => 'sicher','2' => 'unsicher',),
'label' => __("Aboholungsoptionen"),
'required'=>true,
));
// dropdown to show if isPickupDateSure is set to 1
woocommerce_form_field( 'pickupOptionSelection', array(
'type' => 'select',
'required' => 'true',
'class' => array('contactmethod-class form-row-wide'),
'options' => array( // options for <select> or <input type="radio" />
'' => 'Bitte wählen', // empty values means that field is not selected
'nextDays' => 'In den nächsten Tagen', // 'value'=>'Name'
'nextWeek' => 'Nächste Woche',
'next2Weeks' => 'In zwei-drei Wochen',
'nextMonth' => 'Nächsten Monat')
));
// text field which should be shown if isPickupDateSure is 2
woocommerce_form_field('custom_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide') ,
'placeholder' => __('New Custom Field') ,
));
// datepicker to be shown if isPickupDateSure is 2
woocommerce_form_field( 'order_pickup_datetime_local', array(
'type' => 'datetime-local',
'class' => array('my-field-class form-row-wide'),
'required' => true,
'placeholder' => __('Select Date'),
));
}
/**
* Checkout Process
*/
add_action('woocommerce_checkout_process', 'pickupOption_field_process');
function pickupOption_field_process()
{
// Show an error message if the field is not set.
if (!$_POST['isPickupDateSure']) wc_add_notice(__('Please enter value! isPickupDateSure') , 'error');
if (!$_POST['pickupOptionSelection']) wc_add_notice(__('Please enter value! pickupOptionSelection') , 'error');
if (!$_POST['custom_field_name']) wc_add_notice(__('Please enter value! custom_field_name' ) , 'error');
if (!$_POST['order_pickup_datetime_local']) wc_add_notice(__('Please enter value! order_pickup_datetime') , 'error');
}
/**
* Update the value given in custom field
*/
add_action('woocommerce_checkout_update_order_meta', 'pickupOption_field_update_order_meta');
function pickupOption_field_update_order_meta($order_id) {
// TODO foreach
if (!empty($_POST['isPickupDateSure'])) {
update_post_meta($order_id, 'isPickupDateSure',sanitize_text_field($_POST['isPickupDateSure']));
}
if (!empty($_POST['pickupOptionSelection'])) {
update_post_meta($order_id, 'pickupOptionSelection',sanitize_text_field($_POST['pickupOptionSelection']));
}
if (!empty($_POST['order_pickup_datetime_local'])) {
update_post_meta($order_id, 'order_pickup_datetime-local',sanitize_text_field($_POST['order_pickup_datetime_local']));
}
if (!empty($_POST['custom_field_name'])) {
update_post_meta($order_id, 'custom_field_name',sanitize_text_field($_POST['custom_field_name']));
}
}
I have dropdown which generates list of countries in wordpress plugin.
By default it selects first country, i want to change it to poland
What should i do?
if( !get_user_meta( get_current_user_id(), 'user_nationality', true ) ) {
$countries_obj = new WC_Countries();
$countries = $countries_obj->__get('countries');
$fields['billing']['user_nationality'] = array(
'type' => 'select',
'label' => __( 'Nationality', 'mangopay' ),
'options' => $countries,
'required' => true,
);
}
it' my own woocomerce related plugin, i am a beginner.
i followed this as well https://codex.wordpress.org/Creating_Options_Pages.
Use "std" => 'default_value', after 'required' => true,
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.
How can i add a new field in prestashop's back office?
Specific, i want to insert a text field in the BO: Orders->Statuses->Add New Order Status under the status name. Which files i have to modify in order to do that? Can anyone describes the full procedure?
Thanks
I am using Prestashop version 1.6.1.2 and added one text field using following steps. You need to make changes in core files. You have to add field in one table in database and do some changes in class and controller file.
Here are the steps to do the same. I have adde field 'my_custom_field'.
Add one field in order_state table
ALTER TABLE {YOUR_DB_PREFIX}order_state ADD my_custom_field VARCHAR(50) NOT NULL;
Change class file of order state. You need to define your field in file "classes/order/OrderState.php"
After code
public $deleted = 0;
add this code snipet
public $my_custom_field;
After code
'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
add this code snipet
'my_custom_field' => array('type' => self::TYPE_STRING),
open "controllers/admin/AdminStatusesController.php" file and do following changes
in function initOrderStatutsList()
after this code
'name' => array(
'title' => $this->l('Name'),
'width' => 'auto',
'color' => 'color'
),
add this code
'my_custom_field' => array(
'title' => $this->l('My Custom Field'),
'width' => 'auto',
),
in function renderForm()
after this code
array(
'type' => 'text',
'label' => $this->l('Status name'),
'name' => 'name',
'lang' => true,
'required' => true,
'hint' => array(
$this->l('Order status (e.g. \'Pending\').'),
$this->l('Invalid characters: numbers and').' !<>,;?=+()##"{}_$%:'
)
),
add this code
array(
'type' => 'text',
'label' => $this->l('My Custom field'),
'name' => 'my_custom_field',
),
Do changes suggested here. Hope this helps you :)
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.