Created
August 1, 2019 06:27
-
-
Save adambradford/63be28e17c50c58c9f7015d98c62ca70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function adds a custom select field to the campaign submission form. | |
* | |
* @param array $fields | |
* @param Charitable_Ambassadors_Campaign_Form $form | |
* @return array | |
*/ | |
function ed_charitable_add_campaign_form_select_field( $fields, $form ) { | |
/** | |
* Retrieve the current value of the field in the form. | |
*/ | |
$value = $form->get_campaign_value( 'custom_select_field' ); | |
/** | |
* Set up the options that people can choose from in the select. | |
* | |
* You could also get options dynamically from a set of posts, | |
* pages or custom post types, for example. | |
* | |
* @see ed_charitable_get_select_field_options() | |
*/ | |
$args = array( | |
'return' => 'ids', | |
); | |
$products = wc_get_products( $args ); | |
/** | |
* Add the field to the array of fields. | |
*/ | |
$fields['custom_select_field'] = array( | |
'value' => $value, | |
'priority' => 1, // Adjust this to change where the field is inserted. | |
'data_type' => 'meta', | |
'label' => __( 'Custom Field', 'custom-namespace' ), | |
'required' => true, | |
'clear' => false, | |
'type' => 'select', | |
'options' => $options, | |
); | |
return $fields; | |
} | |
add_filter( 'charitable_campaign_submission_campaign_fields' , 'ed_charitable_add_campaign_form_select_field', 10, 2 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @adambradford!
You're on the right track. I'd suggest moving the bit where you get the WooCommerce products to a separate function (the
ed_charitable_get_select_field_options
one below) and then use that for theoptions
when defining the field.You can try the following: