Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Contributor Claudio Sanches

    (@claudiosanches)

    Show me the script.

    Thread Starter claghorn

    (@claghorn)

    /**
    * Add the field to order emails
    **/
    add_filter(‘woocommerce_email_order_meta_keys’, ‘my_woocommerce_email_order_meta_keys’);

    function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys[] = ‘billing_student’;
    $keys[] = ‘student_1’;

    return $keys;
    }

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Thread Starter claghorn

    (@claghorn)

    I tried with no luck:
    /**
    * Add the field to order emails
    **/
    add_filter(‘woocommerce_email_order_meta_keys’, ‘my_custom_checkout_field_order_meta_keys’);

    function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = ‘my_example’;
    return $keys;
    }

    My field is named “billing_student”. I’m using Checkout Field Editor Plugin to create my field.
    https://snag.gy/et4X0.jpg

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    You’re not setting the array key. Look at my snippet again. This part is wrong $keys[]

    Thread Starter claghorn

    (@claghorn)

    /**
    * Add the field to order emails
    **/
    add_filter(‘woocommerce_email_order_meta_keys’, ‘my_woocommerce_email_order_meta_keys’);

    function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys[‘Student’] = ‘billing_student’;
    return $keys;
    }

    Still no bueno ??

    I’m adding it in the functions.php file in my theme directory. Is that the wrong spot?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Ah my bad, my snippet was also out of date ??

    Here is a working one from someone else I found on github you can adjust https://gist.github.com/craigsimps/98adff30f0ac68247d59

    Thread Starter claghorn

    (@claghorn)

    add_filter(‘woocommerce_email_order_meta_fields’, ‘dog_breed_checkout_field_order_meta_fields’, 10, 3 );
    function dog_breed_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields[‘billing_student’] = array(
    ‘label’ => __( ‘Student’ ),
    ‘value’ => get_post_meta( $order->id, ‘billing_student’, true ),
    );
    return $fields;
    }

    Still no luck. Do I need to change more of the script for it to work?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Is it stored as billing_student or _billing_student? it might be the underscored version (hidden).

    https://gist.github.com/mikejolley/8f22c991e80124114d6b

    So in your case, meta_key would be changed to _billing_student. Check postmeta table in the database to confirm the key name.

    Thread Starter claghorn

    (@claghorn)

    changing it to _billing_student worked!!!

    How can I add multiple fields? Thanks

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Repeat this section with a different key:

    $fields['meta_key'] = array(
            'label' => __( 'Label' ),
            'value' => get_post_meta( $order->id, 'meta_key', true ),
        );
    Thread Starter claghorn

    (@claghorn)

    Here is the code for all 3 custom fields:

    add_filter(‘woocommerce_email_order_meta_fields’, ‘dog_breed_checkout_field_order_meta_fields’, 10, 3 );
    function dog_breed_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields[‘_billing_student’] = array(
    ‘label’ => __( ‘Student’ ),
    ‘value’ => get_post_meta( $order->id, ‘_billing_student’, true ),
    );
    $fields[‘_teacher’] = array(
    ‘label’ => __( ‘Teacher’ ),
    ‘value’ => get_post_meta( $order->id, ‘_teacher’, true ),
    );
    $fields[‘_grade’] = array(
    ‘label’ => __( ‘Grade’ ),
    ‘value’ => get_post_meta( $order->id, ‘_grade’, true ),
    );
    return $fields;
    }

    But only Student is coming through on the emails.

    Thread Starter claghorn

    (@claghorn)

    It works if I change my custom field names to billing_teacher, etc.
    I’m wondering if the field name must already contain an “_”? It works and here is the code:

    add_filter('woocommerce_email_order_meta_fields', 'dog_breed_checkout_field_order_meta_fields', 10, 3 );
    function dog_breed_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) {
        $fields['_billing_student'] = array(
                    'label' => __( 'Student' ),
                    'value' => get_post_meta( $order->id, '_billing_student', true ),
                );
        $fields['_billing_teacher'] = array(
                    'label' => __( 'Teacher' ),
                    'value' => get_post_meta( $order->id, '_billing_teacher', true ),
                );
        $fields['_billing_grade'] = array(
                    'label' => __( 'Grade' ),
                    'value' => get_post_meta( $order->id, '_billing_grade', true ),
                );
        return $fields;
    }

    Thanks for all your help!

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    If the fields are hidden (not in the custom fields section of the order), yes _ is needed.

    Hi there, as this is the only place I have found this code working I thought I would ask. this adds the fields to me email; now how would one specify where in the email?

    EX:

    add_filter('woocommerce_email_order_meta_fields', 'abu_add_checkout_field_order_meta_fields', 10, 3 );
    function abu_add_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) {
    
        // ********* These Two I want after the billing_last_name
        $fields['_billing_last_name_2'] = array(
                    'label' => __( '姓(カナ)' ),
                    'value' => get_post_meta( $order->id, '_billing_last_name_2', true ),
                );
        $fields['_billing_first_name_2'] = array(
                    'label' => __( '名(カナ)' ),
                    'value' => get_post_meta( $order->id, '_billing_first_name_2', true ),
                );
    
        // ********These Two I want after the shipping_last_name
        $fields['_shipping_last_name_2'] = array(
                    'label' => __( '姓(カナ)' ),
                    'value' => get_post_meta( $order->id, '_shipping_last_name_2', true ),
                );
        $fields['_shipping_first_name_2'] = array(
                    'label' => __( '名(カナ)' ),
                    'value' => get_post_meta( $order->id, '_shipping_first_name_2', true ),
                );
    
        // ******This one I want the billing_state or wherever, after one is done I am sure I can figure the others
        $fields['_billing_city_address_number'] = array(
                    'label' => __( '市区町村?番地' ),
                    'value' => get_post_meta( $order->id, '_billing_city_address_number', true ),
                );
        $fields['_shipping_city_address_number'] = array(
                    'label' => __( '市区町村?番地' ),
                    'value' => get_post_meta( $order->id, '_shipping_city_address_number', true ),
                );
        return $fields;
    }

    Is there a way to add in the position in this function or will another be required to inject them at specific points?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Custom Checkout Fields’ is closed to new replies.