jarvis Posted January 24, 2017 Share Posted January 24, 2017 Hi, Am hoping someone can help/point me in the right direction! I have the following code: $checkout_fields['order']['categories'] = array( 'type' => 'select', 'class' => array('my-field-class form-row-wide'), 'label' => __('Food options', 'woocommerce'), 'placeholder' => _x('', 'placeholder', 'woocommerce'), 'required' => false, 'clear' => false, 'options' => array( 'eat-meat' => __('I eat maet', 'woocommerce' ), 'not-meat' => __('Meat is gross', 'woocommerce' ) ) ); However, I'd like to make the options part dynamic and therefore grab the values from elsewhere. So I then have this code: $categories = get_field( 'categories', 'options' ); $choices = array(); if ( is_array( $categories ) ) { $category_str = array(); foreach ( $categories as $category ) { #echo $category['value']; #echo $category['label']; $category['value']; $category['label']; $category_str[] = $category['value'].$category['label']; } $result = implode(",",$category_str); echo $result; } My issue is I'm not sure on the best way to amalgamate the two? Can someone help? Quote Link to comment Share on other sites More sharing options...
jarvis Posted January 24, 2017 Author Share Posted January 24, 2017 Ok, I've managed to get somewhere but struggling with one part now. So I now have: $categories = get_field( 'categories', 'options' ); $choices = array(); if ( is_array( $categories ) ) { $category_str = array(); foreach ( $categories as $category ) { $category['value']; $category['label']; $category_str[$category['value']] = $category['label']; } } $checkout_fields['order']['categories'] = array( 'type' => 'select', 'class' => array('my-field-class form-row-wide'), 'label' => __('Food options', 'woocommerce'), 'placeholder' => _x('', 'placeholder', 'woocommerce'), 'required' => false, 'clear' => false, 'options' => array_values($category_str) ); If I use print_r($category_str); I see the following: Array ( [a] => A [b] => B [c] => C ) Yet if I view the drop down it shows: <option value="0">A</option> <option value="1">B</option> <option value="2">C</option> So how do I get the value to be the value from the first array? So it's like this: <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> Thanks Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 24, 2017 Share Posted January 24, 2017 So how do I get the value to be the value from the first array? So it's like this: <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> What does the code for creating the <option> tags look like? Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted January 24, 2017 Solution Share Posted January 24, 2017 Change 'options' => array_values($category_str) to 'options' => $category_str; Quote Link to comment Share on other sites More sharing options...
jarvis Posted January 24, 2017 Author Share Posted January 24, 2017 Hi @cyberRobot It's Wordpress/WooCommerce so it's simply a function/hook: function prg_woocommerce_checkout_fields( $checkout_fields = array() ) { $checkout_fields['order']['categories'] = array( 'type' => 'select', 'class' => array('my-field-class form-row-wide'), 'label' => __('Categories', 'woocommerce'), 'placeholder' => _x('', 'placeholder', 'woocommerce'), 'required' => false, 'clear' => false, 'options' => array_values($category_str) ); return $checkout_fields; } If I manually add the option: 'options' => array( 'eat-meat' => __('I eat meat', 'woocommerce' ), 'not-meat' => __('Meat is gross', 'woocommerce' ) ) Then the drop down creates correctly: <option value="eat-meat">I eat meat</option> <option value="not-meat">Meat is gross</option> But once I try to make it dynamic by passing values from elsewhere, it just doesn't quite display the right output Does that help? Quote Link to comment Share on other sites More sharing options...
jarvis Posted January 24, 2017 Author Share Posted January 24, 2017 Thanks @Barand! God I feel so stupid!!! :-( Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.