[Bug?] Custom field for vendor settings – uncheck a checkbox

We're shifting our Forum based support to a more dedicated support system!

We'll be closing our Forum support from 10th June, 2020 and move to Email Support assistance.

  • If you are a WCFM premium add-ons user, contact us- here
  • Want to know more before buying our add-ons? Send Pre sale queries- here
  • If you are a WCFM free user, please open a support ticket at WordPress.org
  • For WCFM App related queries, reach us- here
From now the forum will be read-only!

Multi Vendor Marketplace Plugin | WCFM Marketplace Forums WCFM – Feature Request [Bug?] Custom field for vendor settings – uncheck a checkbox

Viewing 11 reply threads
  • Author
    Posts
    • #68056

      Dear 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 checkbox isset( $wcfm_settings_form['allow_invoice'] ) evaluates to true, the field is set correctly.
      3. Bug: When unchecking the checkbox isset( $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

    • #69232

      Is there no one who can help me with that?

    • #70178
      WCFM Forum
      Keymaster

      HI,

      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

    • #70502

      Thank 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?

    • #70933
      WCFM Forum
      Keymaster

      HI,

      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

    • #71158

      $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.
    • #71161

      Other 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.

    • #71164

      By 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'.

    • #71189

      Sorry, I meant a bug on ‘type’ => ‘checkbox’.

    • #71278
      WCFM Forum
      Keymaster

      Hi,

      The code I provided you on 29th has field type “checkbox” and this setting is working perfectly – https://ibb.co/J3qM7jR

      Thank You

    • #122276
      developer-9661
      Participant

      Hi,

      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.

      • #122414

        Hello,

        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.

    • #123031
      developer-9661
      Participant

      It works! Thanks!
      You are very helpful!

Viewing 11 reply threads
  • You must be logged in to reply to this topic.