Metabox hidden using the same id

Support MB Conditional Logic Metabox hidden using the same id

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1060
    catarinacarrarocatarinacarraro
    Participant

    I need to make a logical way.

    Apple, Android, Windowsphone

    fields

    apple = iphone, ipad, ipod (fone_id)
    android = galaxy, zenfone, tablet (fone_id)
    windowsphone = nokia, galaxy, etc (fone_id)

    #1075
    Tan NguyenTan Nguyen
    Participant

    Hi catarinaarraro,

    As your logic, this is my snippet, you can edit to fit to your project:

    
    add_filter( 'rwmb_meta_boxes', function( $meta_boxes )
    {
    	$meta_boxes[] = array(
    		'id' 			=> 'brand_product',
    		'title' 		=> 'Brands and Products',
    		'post_types' 	=> array( 'post', 'page' ),
    		'context'		=> 'normal',
    		'priority'		=> 'high',
    		'fields' => array(
    			array(
    				'id'	=> 'fone_id',
    				'name'	=> 'Brand',
    				'desc'	=> 'Pick Your Favourite Brand',
    				'type'	=> 'select',
    				'options' => array(
    					'apple' 		=> 'Apple',
    					'android' 		=> 'Android',
    					'windowsphone' 	=> 'WindowsPhone'
    				)
    			),
    			array(
    				'id' 	=> 'apple_products',
    				'name'	=> 'Select your apple product?',
    				'type'	=> 'checkbox_list',
    				'options' => array('iPhone', 'iPad', 'iPod'),
    				'visible' => array( 'fone_id', '=', 'apple' )
    			),
    			array(
    				'id' 	=> 'android_products',
    				'name'	=> 'Select your android product?',
    				'type'	=> 'checkbox_list',
    				'options' => array('Galaxy', 'ZenPhone', 'Tablet'),
    				'visible' => array( 'fone_id', '=', 'android' )
    			),
    			array(
    				'id' 	=> 'windowsphone_products',
    				'name'	=> 'Select your apple product?',
    				'type'	=> 'checkbox_list',
    				'options' => array( 'Nokia', 'Galaxy' ),
    				'visible' => array( 'fone_id', '=', 'windowsphone' )
    			)
    		)
    	);
    
    	return $meta_boxes;
    } );
    

    Best regards

    Tan Nguyen

    #1078
    catarinacarrarocatarinacarraro
    Participant

    But this way, it writes 3 different id in the database.

    example:
    apple_products
    android_products
    windowsphone_products

    the apple products will record apple_products
    the android products will record android_products
    the windows phone products will record windowsphone_products

    they need to record the same id

    #1080
    catarinacarrarocatarinacarraro
    Participant

    Example:

    will write to the database a unique name, Indepedent which to choose: cel_product

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes )
    {
    	$meta_boxes[] = array(
    		'id' 			=> 'brand_product',
    		'title' 		=> 'Brands and Products',
    		'post_types' 	=> array( 'post', 'page' ),
    		'context'		=> 'normal',
    		'priority'		=> 'high',
    		'fields' => array(
    			array(
    				'id'	=> 'fone_id',
    				'name'	=> 'Brand',
    				'desc'	=> 'Pick Your Favourite Brand',
    				'type'	=> 'select',
    				'options' => array(
    					'apple' 		=> 'Apple',
    					'android' 		=> 'Android',
    					'windowsphone' 	=> 'WindowsPhone'
    				)
    			),
    			array(
    				'id' 	=> 'cel_product',
    				'name'	=> 'Select your apple product?',
    				'type'	=> 'checkbox_list',
    				'options' => array('iPhone', 'iPad', 'iPod'),
    				'visible' => array( 'fone_id', '=', 'apple' )
    			),
    			array(
    				'id' 	=> 'cel_product',
    				'name'	=> 'Select your android product?',
    				'type'	=> 'checkbox_list',
    				'options' => array('Galaxy', 'ZenPhone', 'Tablet'),
    				'visible' => array( 'fone_id', '=', 'android' )
    			),
    			array(
    				'id' 	=> 'cel_product',
    				'name'	=> 'Select your apple product?',
    				'type'	=> 'checkbox_list',
    				'options' => array( 'Nokia', 'Galaxy' ),
    				'visible' => array( 'fone_id', '=', 'windowsphone' )
    			)
    		)
    	);
    	return $meta_boxes;
    } );
    #1088
    Tan NguyenTan Nguyen
    Participant

    Hi catarinacarraro,

    Thanks for your question, unfortunately, we're currently not support that feature yet. We'll mention it to think a way to improve our Conditional Logic.

    Thanks and Best Regards.

    Tan Nguyen

    #1170
    catarinacarrarocatarinacarraro
    Participant

    You're going to solve this?

    #1184
    Tan NguyenTan Nguyen
    Participant

    Hi man,

    Thanks again for sharing your idea.

    Basically, Conditional Logic is the extension to show or hide meta box fields or whole meta box. Each field or meta box id is unique, so we cannot registered two fields or meta boxes at a same time.

    That feature which you've requested is to change the field options (or values) when the conditional is meet, currently, we don't support it but as a great idea, we'll try to implement this feature on the next major update, about late August.

    Best regards

    Tan Nguyen

    #1386
    catarinacarrarocatarinacarraro
    Participant

    You can build it so?

    #1422
    Tan NguyenTan Nguyen
    Participant

    Hi Cararina,

    Sorry for long response, we're still in progress to improving Conditional Logic. As this plugin uses Javascript to handle visibility of object and have to check many field conditional to generate results so it's hard to implements now. In your case, we'll have two ways to make it works natively:

    1. Save it as cel_product_apple, cel_product_android or cel_product_windowsphone in DB then use fone_id to check what's field to output.

    2. Save cel_product_apple, cel_product_android or cel_product_windowsphone in DB, then create another hidden field named cel_product, when post is saved, we also save the visible field to cel_product as an serialized array, then use unserialize function to show up.

    Example (2),

    
    add_filter( 'rwmb_meta_boxes', function( $meta_boxes )
    {
    	$meta_boxes[] = array(
    		'id' 			=> 'brand_product',
    		'title' 		=> 'Brands and Products',
    		'post_types' 	=> array( 'post', 'page' ),
    		'context'		=> 'normal',
    		'priority'		=> 'high',
    		'fields' => array(
    			array(
    				'id'	=> 'fone_id',
    				'name'	=> 'Brand',
    				'desc'	=> 'Pick Your Favourite Brand',
    				'type'	=> 'select',
    				'options' => array(
    					'apple' 		=> 'Apple',
    					'android' 		=> 'Android',
    					'windowsphone' 	=> 'WindowsPhone'
    				)
    			),
    			array(
    				'id' 	=> 'cel_product',
    				'type' 	=> 'hidden',
    			),
    			array(
    				'id' 	=> 'cel_product_apple',
    				'name'	=> 'Select your apple product?',
    				'type'	=> 'checkbox_list',
    				'options' => array('iPhone', 'iPad', 'iPod'),
    				'visible' => array( 'fone_id', 'apple' )
    			),
    			array(
    				'id' 	=> 'cel_product_android',
    				'name'	=> 'Select your android product?',
    				'type'	=> 'checkbox_list',
    				'options' => array('Galaxy', 'ZenPhone', 'Tablet'),
    				'visible' => array( 'fone_id', 'android' )
    			),
    			array(
    				'id' 	=> 'cel_product_windowsphone',
    				'name'	=> 'Select your apple product?',
    				'type'	=> 'checkbox_list',
    				'options' => array( 'Nokia', 'Galaxy' ),
    				'visible' => array( 'fone_id', 'windowsphone' )
    			)
    		)
    	);
    	return $meta_boxes;
    } );
    
    add_action('rwmb_before_save_post', function($post_id)
    {
    	// Get person ID to save from "Select a Customer" field
    	$metabox_to_save = $_POST['fone_id'];
    
    	// Save related field to phone field
    	if ( ! empty( $_POST["cel_product_{$metabox_to_save}"] ) )
    		$_POST['cel_product'] = serialize( $_POST["cel_product_{$metabox_to_save}"] );
    
    	// Clear all hidden meta boxes
    	foreach ( array('apple', 'android', 'windowsphone') as $field )
    	{
    		if ( $field !== $metabox_to_save )
    			unset( $_POST["cel_product_{$field}"] );
    	}
    } );
    

    To retrieve values in front end, just use:

    
    $cel_products = unserialize( rwmb_meta('cel_product') );
    

    Best regards

    Tan Nguyen

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Metabox hidden using the same id’ is closed to new replies.