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

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

This topic contains 9 replies, has 2 voices, and was last updated by WCFM Forum WCFM Forum 3 months, 2 weeks ago.

  • 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
    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
    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
    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

You must be logged in to reply to this topic.