I have added a custom checkout field based on the Customizing checkout fields using actions and filters documentation. The field shows-up in the form but it doesn't but the code to make it required to complete the form doesn't work and the results don't appear in the Order Details.
I'm just curious what I'm doing wrong here:
/** Add the field to the checkout **/
add_action('woocommerce_after_order_notes', 'charity_checkout_field');
function charity_checkout_field( $checkout ) {
echo '<div id="charity_checkout_field"><h3>'.__('Type of Charity').'</h3>';
woocommerce_form_field( 'charity', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Choose Your Type of Charity <abbr class="required" title="required">*</abbr>'),
'placeholder' => _x('charity', 'placeholder', 'woocommerce'),
'options' => array(
'option_1' => '',
'option_2' => 'Church Planting',
'option_3' => 'Community Outreach',
'option_4' => 'Sex Trafficking'),
), $checkout->get_value( 'charity' ));
echo '</div>';
}
/** Process the checkout **/
add_action('woocommerce_checkout_process', 'charity_checkout_field_process');
function charity_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error.
if (!$_POST['charity'])
$woocommerce->add_error( __('Please select the type of charity you want the profits to go to.') );
}
/** Update the order meta with field value **/
add_action('woocommerce_checkout_update_order_meta', 'charity_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['charity']) update_post_meta( $order_id, 'Type of Charity', esc_attr($_POST['charity']));
}
add_action('woocommerce_after_order_notes', 'charity_notes_checkout_field');
function charity_notes_checkout_field( $checkout ) {
echo '<div id="charity_notes_checkout_field" style="font-size: .9em;margin: 5px;padding: 10px; border: 1px dotted #EAEAEA;background-color:#eee;border-radius:5px;">';
echo '<span style="display:block;font: bold 1.2em Verdana,Arial,Helvetica,sans-serif;">Note</span>';
echo '<p>Profits from this sale will go towards one of these charities based on the type you choose above:';
echo ' <ul>';
echo ' <li><strong>Church Planting</strong> - Currently goes into a fund for eventual support for new missionaries at Oral Roberts University.</li>';
echo ' <li><strong>Community Outreach</strong> - <a href="http://www.thelife.cc/#/get-involved/local-outreach" target="_blank">The Life Poindexter</a> in Jackson, MS </li>';
echo ' <li><strong>Sex Trafficking</strong> - <a href="http://lifeimpactintl.org/" target="_blank">Life Impact</a> in Thailand</li>';
echo ' </ul>';
echo'</p>';
echo '</div>';
}
// End custom code