$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - How to include a custom field in the woocommerce email?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - How to include a custom field in the woocommerce email?

matteradmin9PV0评论

I have added a custom field on the checkout page using this:

add_action( 'woocommerce_checkout_fields', 
'woo_add_conditional_checkout_fields' );
function woo_add_conditional_checkout_fields( $fields ) {
foreach( WC()->cart->get_cart() as $cart_item ){
    $product_id = $cart_item['product_id'];
            $fields['billing']['billing_field_newfield'] = array(
                'label'     => __('New Field', 'woocommerce'),
                'placeholder'   => _x('', 'placeholder', 'woocommerce'),
                'required'  => true,
                'class'     => array('form-row-wide'),
                'clear'     => false
            );
        }
return $fields;
}

Now, I would like to include whatever the user enters in this field in the 'New Order' woocommerce email, however when I go to Woocommerce > Settings > Emails > New Order and put {billing_field_newfield} in the subject, I just get {billing_field_newfield} in the email and not its actual value.

Does anyone know how to do this?

Thanks

I have added a custom field on the checkout page using this:

add_action( 'woocommerce_checkout_fields', 
'woo_add_conditional_checkout_fields' );
function woo_add_conditional_checkout_fields( $fields ) {
foreach( WC()->cart->get_cart() as $cart_item ){
    $product_id = $cart_item['product_id'];
            $fields['billing']['billing_field_newfield'] = array(
                'label'     => __('New Field', 'woocommerce'),
                'placeholder'   => _x('', 'placeholder', 'woocommerce'),
                'required'  => true,
                'class'     => array('form-row-wide'),
                'clear'     => false
            );
        }
return $fields;
}

Now, I would like to include whatever the user enters in this field in the 'New Order' woocommerce email, however when I go to Woocommerce > Settings > Emails > New Order and put {billing_field_newfield} in the subject, I just get {billing_field_newfield} in the email and not its actual value.

Does anyone know how to do this?

Thanks

Share Improve this question asked Jan 3, 2019 at 1:25 Bradley JoeBradley Joe 11 silver badge1 bronze badge 2
  • please check url: cloudways/blog/… – vikrant zilpe Commented Jan 3, 2019 at 7:51
  • and docs.woocommerce/document/… – vikrant zilpe Commented Jan 3, 2019 at 8:05
Add a comment  | 

1 Answer 1

Reset to default 1

Adding custom fields to emails

/* 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;
}

Woocommerce documentation: Customizing checkout fields using actions and filters

Post a comment

comment list (0)

  1. No comments so far