Multi Vendor Marketplace Plugin | WCFM Marketplace › Forums › WCFM – Feature Request › [Bug?] Custom field for vendor settings – uncheck a checkbox
Tagged: custom field, filter, vendor page
- This topic has 12 replies, 4 voices, and was last updated 4 years, 10 months ago by
developer-9661.
- AuthorPosts
- June 17, 2019 at 11:26 am #68056
jannis.von.albedyll
ParticipantDear WCLovers,
a couple of days ago a colleague of mine had following question: https://wclovers.com/forums/topic/custom-field-for-vendor-settings/
Unfortunately after some more tests we found out that the function still does not work.We registered following hooks:
add_filter('wcfm_marketplace_settings_fields_general', array($this, 'add_allow_invoice'), 20); add_action('wcfm_vendor_settings_update', array($this, 'save_custom_vendor_settings'), 16, 2);
The ‘add’ function looks as follows:
function add_allow_invoice($settings_fields_general){ $vendor_id = $settings_fields_general['vendor_id']['value']; $allow_invoice = get_user_meta( $vendor_id, 'allow_invoice', true ); $settings_fields_general['allow_invoice'] = array( 'label' => __( 'Rechnung erlauben', 'ddwoo' ), 'type' => 'checkbox', 'priority' => 50, 'class' => 'wcfm-checkbox wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => 'yes', 'dfvalue' => $allow_invoice ); return $settings_fields_general; }
The ‘save’ function is here:
function save_custom_vendor_settings($vendor_id, $wcfm_settings_form) { global $WCFM, $WCFMmp; if( isset( $wcfm_settings_form['allow_invoice'] ) ) { update_user_meta( $vendor_id, 'allow_invoice', 'yes' ); } else { update_user_meta( $vendor_id, 'allow_invoice', 'no' ); } }
This leads to following behaviour:
1. When visiting a vendor-page where the checkbox has never been used, the checkbox is not checked.isset( $wcfm_settings_form['allow_invoice'] )
evaluates to false, as$wcfm_settings_form['allow_invoice']
is null.
2. When checking the checkboxisset( $wcfm_settings_form['allow_invoice'] )
evaluates to true, the field is set correctly.
3. Bug: When unchecking the checkboxisset( $wcfm_settings_form['allow_invoice'] )
still evaluates to true. After a reload the checkbox is checked again. After we have checked the box once it would always evaluate to true (checked or unchecked box).Hint: When debugging the line
if( isset( $wcfm_settings_form['allow_invoice'] ) ) {
we saw that$wcfm_settings_form['allow_invoice']
is always ‘yes’. It does not matter if the checkbox is checked or unchecked. So our guess is that the connection between the checkbox and the form data in the hook might not work correctly (assuming we implemented the ‘add’ function correctly).Do you have any idea how to fix this?
Thank you in advance, we are looking forward to your replay :)!
Jannis - June 24, 2019 at 5:55 am #69232
jannis.von.albedyll
ParticipantIs there no one who can help me with that?
- June 29, 2019 at 6:48 am #70178
WCFM Forum
MemberHI,
Please use this code –
function add_allow_invoice( $settings_fields_general, $vendor_id ) { if( isset( $settings_fields_general['store_name'] ) ) { $allow_invoice = get_user_meta( $vendor_id, 'allow_invoice', true ); $settings_fields_general['allow_invoice'] = array( 'label' => __( 'Rechnung erlauben', 'ddwoo' ), 'type' => 'checkbox', 'priority' => 50, 'class' => 'wcfm-checkbox wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => 'yes', 'dfvalue' => $allow_invoice ); } return $settings_fields_general; } add_filter('wcfm_marketplace_settings_fields_general', array($this, 'add_allow_invoice'), 20, 2 ); function save_custom_vendor_settings($vendor_id, $wcfm_settings_form) { global $WCFM, $WCFMmp; if( isset( $wcfm_settings_form['allow_invoice'] ) ) { update_user_meta( $vendor_id, 'allow_invoice', 'yes' ); } else { update_user_meta( $vendor_id, 'allow_invoice', 'no' ); } } add_action('wcfm_vendor_settings_update', array($this, 'save_custom_vendor_settings'), 16, 2);
Thank You
- July 1, 2019 at 11:42 am #70502
jannis.von.albedyll
ParticipantThank you for your answer, but this does not work :(. The behaviour is still the same:
Once I checked the checkbox,
$wcfm_settings_form['allow_invoice']
is ALWAYS yes (no matter if I uncheck the box or not).If I uncheck it and save the settings, I do not enter the else path
else { update_user_meta( $vendor_id, 'allow_invoice', 'no' ); }
Do you have any other idea?
- July 4, 2019 at 5:21 am #70933
WCFM Forum
MemberHI,
I have test this and it’s working perfectly.
What you are getting data from here ? –
$allow_invoice = get_user_meta( $vendor_id, 'allow_invoice', true );
Thank You
- July 5, 2019 at 8:11 am #71158
jannis.von.albedyll
Participant$allow_invoice = get_user_meta( $vendor_id, 'allow_invoice', true );
always gives me yes, after I checked the checkbox once.Even if I UNcheck the box, the code evaluates ‘yes’ (see screenshot attached).
So the connection between the checkbox and the
add_action('wcfm_vendor_settings_update', array($this, 'save_custom_vendor_settings'), 16, 2);
does not work properly as the code always evaluates ‘yes’ for$wcfm_settings_form['allow_invoice']
.Attachments:
You must be logged in to view attached files. - July 5, 2019 at 8:13 am #71161
jannis.von.albedyll
ParticipantOther fields of
$wcfm_settings_form
get updated properly in the add_action. So if I change something about the address for instance, in debugging the new value is submitted. - July 5, 2019 at 8:38 am #71164
jannis.von.albedyll
ParticipantBy the way: following custom-field works perfectly:
function add_allow_invoice($settings_fields_general, $vendor_id){ if( isset( $settings_fields_general['store_name'] ) ) { $allow_invoice = get_user_meta( $vendor_id, 'allow_invoice', true ); $settings_fields_general['allow_invoice'] = array( 'label' => __( 'Rechnung erlauben', 'ddwoo' ), 'type' => 'text', 'priority' => 50, 'class' => 'wcfm-text wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => $allow_invoice ); } return $settings_fields_general; }
The value gets always updated and is persisted correctly. That’s why I guess it’s a bug on the
'type' => 'text'
. - July 5, 2019 at 11:28 am #71189
jannis.von.albedyll
ParticipantSorry, I meant a bug on ‘type’ => ‘checkbox’.
- July 6, 2019 at 8:46 am #71278
WCFM Forum
MemberHi,
The code I provided you on 29th has field type “checkbox” and this setting is working perfectly – https://ibb.co/J3qM7jR
Thank You
- April 23, 2020 at 10:36 pm #122276
developer-9661
ParticipantHi,
I have the same issue with checkbox, I’ve added this code with plugin code-snippets:
function add_allow_invoice( $settings_fields_general, $vendor_id ) { if( isset( $settings_fields_general['store_name'] ) ) { $allow_invoice = get_user_meta( $vendor_id, 'allow_invoice', true ); $settings_fields_general['allow_invoice'] = array( 'label' => __( 'Rechnung erlauben', 'ddwoo' ), 'type' => 'checkbox', 'priority' => 50, 'class' => 'wcfm-checkbox wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => 'yes', 'dfvalue' => $allow_invoice ); } return $settings_fields_general; } add_filter('wcfm_marketplace_settings_fields_general', 'add_allow_invoice', 20, 2 ); function save_custom_vendor_settings($vendor_id, $wcfm_settings_form) { global $WCFM, $WCFMmp; if( isset( $wcfm_settings_form['allow_invoice'] ) ) { update_user_meta( $vendor_id, 'allow_invoice', 'yes' ); } else { update_user_meta( $vendor_id, 'allow_invoice', 'no' ); } } add_action('wcfm_vendor_settings_update', 'save_custom_vendor_settings', 16, 2);
But just like Jannis said, even if I unchecked the checkbox, the value of isset( $wcfm_settings_form[‘allow_invoice’] always return ‘yes’.
And when I changed it to type = ‘text’, everything works fine. Please help.- April 24, 2020 at 12:04 pm #122414
Sarmistha Chakraborty
MemberHello,
Modify the code,
function add_allow_invoice( $settings_fields_general, $vendor_id ) { $vendor_data = get_user_meta( $vendor_id, 'wcfmmp_profile_settings', true ); $allow_invoice = isset( $vendor_data['allow_invoice'] ) ? esc_attr( $vendor_data['allow_invoice'] ) : 'no'; if( isset( $settings_fields_general['store_name'] ) ) { //$allow_invoice = get_user_meta( $vendor_id, 'allow_invoice', true ); $settings_fields_general['allow_invoice'] = array( 'label' => __( 'Rechnung erlauben', 'ddwoo' ), 'type' => 'checkbox', 'priority' => 50, 'class' => 'wcfm-checkbox wcfm_ele', 'label_class' => 'wcfm_title wcfm_ele', 'value' => 'yes', 'dfvalue' => $allow_invoice ); } return $settings_fields_general; } add_filter('wcfm_marketplace_settings_fields_general', 'add_allow_invoice', 20, 2 ); function save_custom_vendor_settings($vendor_id, $wcfm_settings_form) { global $WCFM, $WCFMmp; $wcfm_settings_form_data_new = array(); parse_str($_POST['wcfm_settings_form'], $wcfm_settings_form_data_new); $wcfm_settings_form_data_storetype = array(); if(isset($wcfm_settings_form_data_new['allow_invoice']) && !empty($wcfm_settings_form_data_new['allow_invoice'])) { $wcfm_settings_form_data_storetype['allow_invoice'] = 'yes'; } else { $wcfm_settings_form_data_storetype['allow_invoice'] = 'no'; } $wcfm_settings_form = array_merge( $wcfm_settings_form, $wcfm_settings_form_data_storetype ); update_user_meta( $vendor_id, 'wcfmmp_profile_settings', $wcfm_settings_form ); } add_action('wcfm_vendor_settings_update', 'save_custom_vendor_settings', 30, 2); add_action( 'wcfm_wcfmmp_settings_update','save_custom_vendor_settings', 30, 2);
Thanks.
- April 26, 2020 at 12:22 am #123031
developer-9661
ParticipantIt works! Thanks!
You are very helpful!
- AuthorPosts
- You must be logged in to reply to this topic.