0

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.

0

I don't know how I missed the answer, it's way more simple than the stuff I was trying.

From the docs:

/* To use: 
1. Add this snippet to your theme's functions.php file
2. Change the meta key names in the snippet
3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg
4. When next updating the status, or during any other event which emails the user, they will see this field in their email
*/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_order_meta_keys');

function my_custom_order_meta_keys( $keys ) {
     $keys[] = 'Tracking Code'; // This will look for a custom field called 'Tracking Code' and add it to emails
     return $keys;
}

in my case

add_filter('woocommerce_email_order_meta_keys', 'client_already_email_yes');

function client_already_email_yes( $keys ) {
     $keys[] = 'Client Already:YES'; // This will look for a custom field called 'Tracking Code' and add it to emails
     return $keys;
}

This returns a value of 1 from the checked checkbox and sticks it at the end of the text in the email. It's not pretty but good enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.