I'm searching a way of creating new placeholders for subject and recipients email when a new order is create? I have created new fields with ACF : "chantier" and "conducteur de travaux" called and I want to be able to add {chantier} in my subject mail and add {conducteur} to my recipient.
Here my code for ACF field :
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5c49b50b45a52',
'title' => 'Nom du chantier',
'fields' => array(
array(
'key' => 'field_5c49b5127665d',
'label' => 'Chantier',
'name' => 'chantier',
'type' => 'post_object',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'show_fields_options' => array(
0 => 'order',
1 => 'email',
),
'post_type' => array(
0 => 'chantier',
),
'taxonomy' => '',
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'object',
'ui' => 1,
),
array(
'key' => 'field_5c541edb71030',
'label' => 'Conducteur de travaux',
'name' => 'conducteur_de_travaux',
'type' => 'user',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'show_fields_options' => array(
0 => 'order',
1 => 'email',
),
'role' => array(
0 => 'conducteur-de-travaux',
),
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'array',
),
),
'location' => array(
array(
array(
'param' => 'checkout',
'operator' => '==',
'value' => 'wc-before-billing-form',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
I have tried with this code suggested here :
Create additional variables/placeholders for Woocommerce email notifications subject
But it doesn't works, here my code :
add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
function add_custom_email_format_string( $string, $email ) {
// The post meta key used to save the value in the order post meta data
$meta_key = '_chantier';
// Get the instance of the WC_Order object
$order = $email->object;
// Get the value
$value = get_field( $meta_key, $order_id );
// Additional subject placeholder
$new_placeholders = array( '{chantier}' => $value );
// Return the clean replacement value string for "{chantier}" placeholder
return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}
When I receive the mail I have it : Order {chantier}. I don't know where is my mistake...
And for the field "conducteur-de-travaux" (user field), I would like to recover the email of the "conducteur de travaux" and add it to the new order recipient email. Is it possible?