I am trying to use the filter woocommerce_get_country_locale to remove the "State" field for a specific country. However, it does not work.
Here is what I tried:
Attempt 1
add_filter(
'woocommerce_get_country_locale',
static function (array $locale): array {
$locale['HK']['state']['hidden'] = true;
return $locale;
}
);
Attempt2
apply_filters(
'woocommerce_get_country_locale',
[
'HK' => [
'postcode' => [
'required' => false,
],
'city' => [
'label' => __('Town / District', 'woocommerce'),
// 'placeholder' => __( 'Town / District', 'woocommerce' )
],
],
]
);
Please suggest how to achieve this. Thank you!
You could try adding to your array:
'state' => array(
'required' => false
)
Like France in the function, for example.
So your filter would look like
apply_filters( 'woocommerce_get_country_locale', array(
'HK' => array(
'postcode' => array(
'required' => false
),
'city' => array(
'label' => __( 'Town / District', 'woocommerce' ),
//'placeholder' => __( 'Town / District', 'woocommerce' )
)
'state' => array(
'required' => false
)
)
));
The function which controls this lives in
woocommerce/includes/class-wc-countries.php
It could be removed there but you would need to take great care to ensure that this is maintained when you update WooCommerce. I would not recommend editing core theme files in this way but it can produce the desired result.
This might help in providing good background to the get country locale function:
http://woocommerce.wp-a2z.org/oik_api/wc_countriesget_country_locale/?bwscid1=2
And this might provide another way - showing that that country is not allowed if you wanted to remove HK altogether:
http://woocommerce.wp-a2z.org/oik_api/wc_countriesget_allowed_countries/
Something in the answers here might help to point you in a more correct way to do it:
https://wordpress.stackexchange.com/questions/73062/how-to-force-wordpress-to-temporarily-switch-locale-using-qtranslate
This also might explain some of the behaviour: https://wordpress.stackexchange.com/questions/120741/cant-change-a-label-in-woocommerce-with-the-normal-filter
Related
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();
}
I am currently developing an elementor extension, and I have the following problem: I'd like to set the repeater item's title to the current value of a select. if my select control is titled 'country' and i use {{{country}}} lateron, I just get 1,2,3,4,5
I cannot find any info about this in the elementor documentation
require_once(plugin_dir_path(__FILE__).'countries.php');
$this->start_controls_section(
'content_section',
[
'label' => __( 'Content', 'plugin-name' ),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$repeater = new \Elementor\Repeater();
$repeater->add_control(
'country',
[
'label' => __('Country Name', 'elementor-cc-details'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => '',
'options' => $countries,
'label_block' => true
]
);
$repeater->add_control(
'list_content', [
'label' => __( 'Content', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::WYSIWYG,
'default' => __( 'List Content' , 'plugin-domain' ),
'show_label' => false,
]
);
$this->add_control(
'list',
[
'label' => __( 'Repeater List', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::REPEATER,
'fields' => $repeater->get_controls(),
'default' => [
],
'title_field' => '{{{ country}}}',
]
);
$this->end_controls_section();
}
The expected results are, that if I select a value in the <select>, the repeater item get's its value as name. Maybe its {{{ country.select }} } or something. I didn't find it out.
I solved my problem with a workaround. the real problem is not fixed yet, as you can not chose between key and value.
The temporary solution was to use the countries array like this
$countries = [
"Afghanistan" => "Afghanistan",
"Albania" => "Albania",
"Algeria" => "Algeria",
"Andorra" => "Andorra",
"Angola" => "Angola",
...
because the {{{ country }}} variable automatically uses the array key (if not existent 1,2,3,4,5).
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.
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.
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.