Showing a description under input field on WooCommerce Checkout Page - php

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.

Related

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

select a default value in php wordpress plugin development

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,

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/

Readonly form fields

So I have an PHP file that includes a form so that users can post jobs on my website. I want to make some fields of this form ReadOnly to the user. How?
public static function init_fields() {
if ( self::$fields )
return;
self::$fields = apply_filters( 'submit_job_form_fields', array(
'job' => array(
'job_category' => array(
'label' => __( 'Job category', 'job_manager' ),
'type' => 'select',
'required' => true,
'options' => self::job_categories(),
'placeholder' => '',
'priority' => 3
),
'job_description' => array(
'label' => __( 'Description', 'job_manager' ),
'type' => 'text',
'required' => true,
'placeholder' => '',
'priority' => 4
),
I know this is probably easy to do, but for some reason I can't find how to do it.
Use the disabled attribute:
'job_description' => array(
'label' => __( 'Description', 'job_manager' ),
'type' => 'text',
'required' => true,
'placeholder' => '',
'priority' => 4,
'disabled' => 'true' //html disabled input
)
This is a proprietary config file. You need to look at the code that is parsing it and converting to HTML. It may allow passing through variables like readonly, or may not.
Try passing 'readonly' => true in your array.
Remember that someone can change the value of the readonly field using an inspector like Firebug, Chrome Developer Tools, etc.

Categories