WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Add a custom field (in an order) to the emails (4 posts)

  1. simmonsstummer
    Member
    Posted 1 month ago #

    Hi,
    i need to add the value of a custom field in the checkout to the mail i receive.

    this is what says the codex but does not show the value i need:

    /**
     * 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['How did you hear about us?'] = 'hear_about_us';
    	return $keys;
    }

    https://wordpress.org/plugins/woocommerce/

  2. Daniel Espinoza
    Member
    Posted 1 month ago #

    The code that uses the woocommerce_email_order_meta_keys filter is in
    /includes/class-wc-emails.php

    Here it is:

    if ( $show_fields )
                            foreach ( $show_fields as $key => $field ) {
                                     if ( is_numeric( $key ) )
                                             $key = $field;
    
                                     $meta[ wptexturize( $key ) ] = wptexturize( get_post_meta( $order->id, $field, true ) );
                             }

    You'll see it's checking that the $key is numeric. Your key is 'How did you hear about us?' and is not numeric.

    The original gist is here: https://gist.github.com/ChromeOrange/3905785

    Try removing that key and give it another try.

  3. simmonsstummer
    Member
    Posted 1 month ago #

    how can i ovveride that code without editing the class-wc-emails.php?

  4. Daniel Espinoza
    Member
    Posted 1 month ago #

    You shouldn't override core code. The gist shows how to hook in with an action and use a filter. The core code is written in a way that allows it to be extended in this way.

Reply

You must log in to post.

About this Plugin

About this Topic