Home Forums WC Vendors Pro Support Form helper & multi select

NOTICE: We've Moved to a Ticket System for Support

As of August 31, 2017 (12am EST) our support forums will be retired (read-only), and we will be moving to a support ticket system.  This will allow us to better organize and answer support requests, and provide a more personalized experience as we assist our customers.

For the time being, we will leave our forums open for reading and learning while we work on creating a more robust Knowledge Base for everyone to use.

If you are a WC Vendors Pro customer please open a support ticket here. 

If you are a WC Vendors user please open a support ticket on the Wordpress.org forums.

The information on this forum is outdated and in most instances no longer relevant. Please be sure to check our documentation for the most up to date information.

https://docs.wcvendors.com/

Thank you to all of our customers!

 

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #15146
    Stephen
    Participant

    Maybe I’m missing something, or just going bogeyed with fatigue. The select form helper allows you to add multiple as a custom attribute to turn it into a multi-select, but I don’t see a way of an array of values being recognises during parsing that will display them again once saved.

    This is the code for the product-edit template

    
            $usage_hire_value = (array)get_post_meta( $object_id, '_wcv_custom_product_usage_hire', true );
            print_r( $usage_hire_value );
    
            $usage_hire_attributes = array( 'multiple' => 'multiple' );
            $usage_hire_options = array (  
                'holiday'   => __( 'Holiday Hire', 'wcvendors-pro' ),  
                'wedding'   => __( 'Wedding Hire', 'wcvendors-pro' ),  
                'prom'      => __( 'Prom & Ball Hire', 'wcvendors-pro' )
           	); 
    
            WCVendors_Pro_Form_Helper::select( apply_filters( 'wcv_custom_product_usage_hire', array( 
    			'post_id'			=> $object_id, 
    			'id' 				=> '_wcv_custom_product_usage_hire[]', 
    			'label' 			=> __( 'What can I do?', 'wcvendors-pro' ), 
    			'wrapper_start' 	=> '<div class="all-100">',
    			'wrapper_end' 		=> '</div>', 
                'options' 			=> $usage_hire_options,
                'custom_attributes' => $usage_hire_attributes, 
                'value'             => $usage_hire_value
    			) )
            );
    

    and capturing the result via the save meta hook.

    
    add_action( 'wcv_save_product_meta', 'riv_pro_save_product_meta', 10, 1 );
    function riv_pro_save_product_meta ( $product_id ) {
    
        // product meta - usage hire
        if ( isset( $_POST[ '_wcv_custom_product_usage_hire' ] ) ) { 
    
    		$usage_hire = $_POST[ '_wcv_custom_product_usage_hire' ];
            print_r( $usage_hire);
    
    	    if ( ! empty( $usage_hire ) ) {
                foreach ( $usage_hire as $hire ) { $hire = wc_clean( $hire ); }
                update_post_meta( $product_id, '_wcv_custom_product_usage_hire', $usage_hire );    
    		} else {
               delete_post_meta( $product_id, '_wcv_custom_product_usage_hire' );    
    		}
        }    
    }
    

    A bit clunky atm but works. But… when reloading the page the meta is retrieved as an array but the select wont parse it as such. Is there a better way of doing this and still have the ability to multi select from a list?

    Thanks

    #15151
    WC Vendors Support
    Participant

    @digitalchild would be better to ask than me on this one. 🙂

    #15183
    Stephen
    Participant

    Ok, is he around. Can he look at this?

    Thanks

    #15184
    Jamie
    Keymaster

    Hi Stephen,

    You’re casting it as an array which is why its doing that.

    $usage_hire_value = (array)get_post_meta( $object_id, ‘_wcv_custom_product_usage_hire’, true );

    should be

    $usage_hire_value = get_post_meta( $object_id, ‘_wcv_custom_product_usage_hire’, true );

    cheers,

    Jamie.

    #15190
    Stephen
    Participant

    Hi Jamie

    Thanks. No difference. It’s already coming through as an array – or should be – so the casting is there more as a sanity check. It’s what happens at the form helper :: select that’s a potential issue.

    It uses selected() as the check, but afaik this only works as a check for non-array values, so when it iterates through the field options the key/value check won’t set any as selected. e.g. select form helper @230. There’s some checking if it’s a taxonomy but not a meta data array value.

    
    foreach ( $field['options'] as $key => $value ) {
    	echo '<option value="' . esc_attr( $key ) . '" ' . selected( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
    }
    
    #15194
    Stephen
    Participant

    Hi. Changing the select check to allow for array value sorts it

    
    echo '<option value="' . esc_attr( $key ) . '" ' . selected( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
    

    to

    
    $selected = ( is_array( $field['value'] ) ) ? ( in_array( esc_attr( $key ), $field['value'] ) ) ? 'selected="selected"' : '' : selected( esc_attr( $field['value'] ), esc_attr( $key ), false );
    echo '<option value="' . esc_attr( $key ) . '" ' . $selected . '>' . esc_html( $value ) . '</option>';
    

    Will be needed for select, select2 & prob nested_select. I can log a github issue if needed.

    S

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘WC Vendors Pro Support’ is closed to new topics and replies.