Metabox hidden using the same id
Support › MB Conditional Logic › Metabox hidden using the same id
- This topic has 8 replies, 2 voices, and was last updated 9 years, 7 months ago by
Tan Nguyen.
-
AuthorPosts
-
July 10, 2015 at 3:13 AM #1060
catarinacarraro
ParticipantI 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)July 14, 2015 at 7:08 PM #1075Tan Nguyen
ParticipantHi 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
July 16, 2015 at 1:25 AM #1078catarinacarraro
ParticipantBut this way, it writes 3 different id in the database.
example:
apple_products
android_products
windowsphone_productsthe apple products will record apple_products
the android products will record android_products
the windows phone products will record windowsphone_productsthey need to record the same id
July 16, 2015 at 2:51 AM #1080catarinacarraro
ParticipantExample:
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; } );
July 17, 2015 at 3:20 PM #1088Tan Nguyen
ParticipantHi 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
August 6, 2015 at 3:01 AM #1170catarinacarraro
ParticipantYou're going to solve this?
August 8, 2015 at 4:37 AM #1184Tan Nguyen
ParticipantHi 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
September 4, 2015 at 2:56 AM #1386catarinacarraro
ParticipantYou can build it so?
September 11, 2015 at 3:02 PM #1422Tan Nguyen
ParticipantHi 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
orcel_product_windowsphone
in DB then usefone_id
to check what's field to output.2. Save
cel_product_apple
,cel_product_android
orcel_product_windowsphone
in DB, then create another hidden field namedcel_product
, when post is saved, we also save the visible field tocel_product
as an serialized array, then useunserialize
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
-
AuthorPosts
- The topic ‘Metabox hidden using the same id’ is closed to new replies.