Skip to content

Instantly share code, notes, and snippets.

@adambradford
Created August 1, 2019 06:27
Show Gist options
  • Save adambradford/63be28e17c50c58c9f7015d98c62ca70 to your computer and use it in GitHub Desktop.
Save adambradford/63be28e17c50c58c9f7015d98c62ca70 to your computer and use it in GitHub Desktop.
/**
* 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 );
@ericnicolaas
Copy link

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 the options when defining the field.

You can try the following:

/**
 * 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' );

    /**
     * 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'   => ed_charitable_get_select_field_options(),
    );

     return $fields;
}

add_filter( 'charitable_campaign_submission_campaign_fields' , 'ed_charitable_add_campaign_form_select_field', 10, 2 );

/**
 * Get the WooCommerce products.
 *
 * @return array
 */
function ed_charitable_get_select_field_options() {
    /**
     * @see https://codex.wordpress.org/Class_Reference/WP_Query
     */
    $args = array(
        'return' => 'ids',
    );

    $products = wc_get_products( $args );

    $options = array();

    foreach ( $products as $post_id ) {
        $options[ $post_id ] = get_post_field( 'post_title', $post_id );
    }

    return $options;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment