Custom field for vendor settings

This topic contains 5 replies, has 2 voices, and was last updated by WCfM Forum WCfM Forum 2 days, 11 hours ago.

  • Author
    Posts
  • #66015
    Avatar
    matthias.koch
    Participant

    Hello,

    I am not sure whether there is a bug or I missed something in my attempt to add a custom settings field to the vendor settings page.
    I used this hook
    add_filter('wcfm_marketplace_settings_fields_general', array($this, 'add_allow_invoice'), 20);
    to add a checkbox like this:

    	function add_allow_invoice($settings_fields_general){
    		$vendor_id = $settings_fields_general['vendor_id']['value'];
    		$vendor_data = get_user_meta( $vendor_id, 'wcfmmp_profile_settings', true );
    		if( !$vendor_data ) $vendor_data = array();
    		$allow_invoice = isset( $vendor_data['allow_invoice'] ) ? esc_attr( $vendor_data['allow_invoice'] ) : 'no';
    		$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;
    	}

    So far, so good, the checkbox appears and I can check it. When I check it and save it, the value gets stored.

    However, if I uncheck the previously checked box and save the form, the value does not get set to “no”, because the “wcfm_settings_form” in “data” in line 147 in the file \wp-content\plugins\wc-frontend-manager\assets\js\vendors\wcfm-script-vendors-manage.js does not contain the checkbox at all. During runtime, it is only included in the serialized form when I checked it.
    Without having digged deeper so far, I assume, for fields added via the filter, there is an issue with checkboxes once they are unchecked.
    Could you please have a look at the issue? I would be very happy if you could help me. 🙂

    Thank you and best regards
    Matthias

  • #66261
    WCfM Forum
    WCfM Forum
    Keymaster

    HI,

    Please use this code save this field separately –

    add_action( 'wcfm_vendor_settings_update', function( $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',  'yes' );
            }
    }, 500, 2 );

    and directly read from meta –

    get_user_meta( $vendor_id, 'allow_invoice', true );

    Thank You

  • #66361
    Avatar
    matthias.koch
    Participant

    Thank you for your reply! This brings me already one step forward.

    Sadly, I have one more issue:

    	function save_custom_vendor_settings($vendor_id, $wcfm_settings_form) {
    		global $WCFM, $WCFMmp;
    		if( isset( $wcfm_settings_form['allow_invoice'] ) ) {
    			$a = $wcfm_settings_form['allow_invoice']; //ISSUE: Always "yes"
    			update_user_meta( $vendor_id, 'allow_invoice',  $wcfm_settings_form['allow_invoice'] );
    		} else {
    			update_user_meta( $vendor_id, 'allow_invoice',  'no' );
    		}
    	}

    In the line where (for testing only) I set the $a variable, I notice that the value of $wcfm_settings_form['allow_invoice'] is always “yes”, even though I have the checkbox unchecked.
    Did I miss something else?

    Thank you again for your support!
    Regards
    Matthias

  • #66852
    WCfM Forum
    WCfM Forum
    Keymaster

    HI,

    Sorry, my code was wrong. Here is corrected one –

    add_action( 'wcfm_vendor_settings_update', function( $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' );
            }
    }, 500, 2 );

    Thank You

  • #67181
    Avatar
    matthias.koch
    Participant

    This works perfectly now, thank you very much!

  • #67415
    WCfM Forum
    WCfM Forum
    Keymaster

    Glad to here, you are welcome 🙂

The topic ‘Custom field for vendor settings’ is closed to new replies.