I’ve added a checkbox to the checkout page:
add_action('woocommerce_after_order_notes', 'client_already_field');
function client_already_field( $checkout ) {
echo '<div id="client-already-field"><h3>'.__('CLIENT ALREADY? ').'</h3>';
woocommerce_form_field( 'client_already_checkbox_yes', array(
'type' => 'checkbox',
'class' => array('input-checkbox-yes'),
'label' => __('YES!'),
'required' => false,
), $checkout->get_value( 'client_already_checkbox_yes' ));
echo '</div>';
}
And saved it to order meta (both of these functions worked):
//1
add_action('woocommerce_checkout_update_order_meta', 'client_already_order_meta_yes');
function client_already_order_meta_yes( $order_id ) {
if ($_POST['client_already_checkbox_yes']) update_post_meta( $order_id, 'Client Already:Yes', esc_attr($_POST['client_already_checkbox_yes']));
}
// 2
function client_already_order_meta_yes( $order_id ) {
if( !empty( $_POST['client_already_checkbox_yes'] ) && $_POST['client_already_checkbox_yes'] == 1 )
update_post_meta( $order_id, 'Client Already:YES', 1 );
}
But when I tick the box and complete a transaction nothing shows in the admin order received email. Things I’ve tried:
// 1
add_filter( 'woocommerce_email_order_meta_fields', 'client_already_email', 10, 3 );
function client_already_email( $fields, $sent_to_admin, $order_obj ) {
$is_yes = get_post_meta( $order_obj->get_order_number(), 'client_already_checkbox_yes', true );
if( empty( $is_yes ) )
return $fields;
$fields['client_already_checkbox_yes'] = array(
'label' => __( 'Client Already' ),
'value' => 'Yes'
);
return $fields;
}
// 2
function client_already_email( $fields, $sent_to_admin, $order_obj ) {
$is_yes = get_post_meta( $order_obj->get_order_number(), 'client_already_checkbox_yes', true );
if( empty( $is_yes ) )
return $fields;
echo "Client Already? Yes";
);
return $fields;
}
// 3
function client_already_email( $fields, $sent_to_admin, $order ) {
$fields['client_already_checkbox_yes'] = array(
'label' => __( 'Client Already? YES' ),
'value' => get_post_meta( $order->id, 'client_already_checkbox_yes', true ),
);
return $fields;
}
I’ve tried a few other similar things too. I know I’m missing something involved with properly retrieving and evaluating the value from the checkbox and sticking that value into the email once it’s known.
I’ve put a few other fields into this checkout page that are being saved to meta and added to transaction emails, not sure what I’m missing here.
I’m not so hot with PHP so I’m treading in familiar but not comfortable territory.