Conditional visibility not working with checkboxes
-
I have a number field that i only want to appear if a specific checkbox is checked. There doesn’t seem to be a way to set the logic to check this. It always reads the checkbox as true/1/not empty whether it is checked or not, meaning the number field is always visible and no way to conditionaly hide/show it.
-
Hi, fixed in v.1.4.4. Rule “{checkbox} has any value” should work now.
@wckalkulator Works perfectly, thank you!
I did notice another bug you may want to be aware of. I also had tried with multi-checkboxes. I wanted to have, for example, 5 checkboxes and a number field that becames visible for each checkbox. So, if checkbox 1, 3 and 4 were checked, number fields 1, 3 and 4 would become visible. This didn’t work for me using manual values, “is empty” or “has any value”. If you can get this working it’d be a big help for a client i currently have and I know you don’t do a paid version but i’ll hapilly give a donation if you give me a few days.
Hi, I will do it in 12 h. Thanks for this information
@castria multi checkbox has beed fixed in 1.4.5. For example: {multi_cb} has values: 10 -> Option A, 20 -> Option B. So the visibility rule for a first number field will be “{multi_cb} == 10” and the second one “{multi_cb} == 20”
Thank you so much. Incredible support for a free plugin. That works great now.
I’ve now encountered a new problem. In price calculation, I chose “Price Add-ons”. I want it to be:
if {multi_cb} == 1, add {number_field_1} * 100 if {multi_cb} == 2, add {number_field_2} * 185 if {multi_cb} == 3, add {number_field_3} * 220but doing this doesn’t work and i get the following message on the product screen:
“Total Variable “multi_cb” is not valid around position 1 for expression “multi_cb == 1”. Did you mean “number_field_1″?”
It does work if my conditional checks if the number fields are >= 1 but the problem there is they have a minimum value of 1, so if the checkbox is selected, it would add the price, but if it was then deselected, it becomes invisible but the price doesn’t go back to 0, so it only works properly if the price is only set to be added IF the relevant checkbox is checked in the multi checkbox.
Multi checkbox is tricky, because it returns fixed price or sum/min/max of selected values. Try something like if {num1} > 0 add {num1}*100.
My idea was to make function something like is_selected({multicb}, 1), maybe you have better idea?
My idea was to make function something like is_selected({multicb}, 1), maybe you have better idea?
Yeah this is my thinking for what should be the simplest solution.
Ok, try install this plugin from github (https://github.com/piatkowski/wckalkulator/tree/1.4.6). I’ve added
is_selectedfunction, example usage:if is_selected(10, {multi_cb})may betrueorfalse.Please give me feedback before I update plugin in WP.org repository.
Ok, I installed the github version.
The add-on price calculation is as follows ({aps} is multi checkbox, ap1 is the number field that appears when the first checkbox is checked):
if is_selected(1,{aps}) add {ap1} * 100I get the following error message on the product page:
“Total Arguments must be separated by a comma. Unexpected token “punctuation” of value “.” (“punctuation” expected with value “,”) around position 14 for expression
is_selected(1.aps).”-
This reply was modified 3 years, 7 months ago by
castria.
My mistake, should be
is_selected({field}; value)and in your case this isis_selected({aps}; 1). Arguments are separated by semicolon, because numbers can be written as 1.23 or 1,23 (we use comma in Poland).Ah that makes sense. So functionally that is working now. The only issue i’m still seeing is that if nothing is selected in the multicheckbox, i get the error message
“Total Variable “aps” is not valid around position 13 for expression
is_selected(aps, 1).”As soon as at least one is checked the error message goes away and it works as intended.
Please give me screen shot of config. Do you use
;to separate arguments ? I tested it and if no option is selected thenapswill be empty array(). This error shoud not occur.You can also check file src/FieldsetProduct.php lines 349 to 352, should be:
/* Set Default values if the field is not in POST data */ if($this->field($name)['type'] === 'checkboxgroup') { $filtered_post[$name] = array(); }I can’t find bug on my local dev env.
Yes, I used a semicolon. I’ve double checked the file you mentioned and it shows exactly as you wrote.
Here is a gallery showing my configs and how the product page shows when nothing is checked vs when something is checked.
Ok, I found that issue was when multicheckbox was the only field.
Try this updated plugin: https://github.com/piatkowski/wckalkulator/releases/tag/1.4.6You should not check number fields as required, because it does not pass the validation test. I plan to change behaviour of “required” option to be: 1. always require, 2. require when visible, 3. not required
At this moment you can set multi checkbox as required instead of number fields.
That works perfectly now. Thanks so much for your efforts! I’ll send a review and a donation your way by today/tomorrow.
-
This reply was modified 3 years, 7 months ago by
The topic ‘Conditional visibility not working with checkboxes’ is closed to new replies.