Php 具有ACF的Woocommerce电子邮件通知主题的其他占位符

Php 具有ACF的Woocommerce电子邮件通知主题的其他占位符,php,wordpress,woocommerce,advanced-custom-fields,email-notifications,Php,Wordpress,Woocommerce,Advanced Custom Fields,Email Notifications,我正在搜索一种在创建新订单时为主题和收件人电子邮件创建新占位符的方法?我已经用ACF创建了新字段:“chantier”和“conducteur de travaux”,我希望能够在我的主题邮件中添加{chantier},并将{conducteur}添加到我的收件人 这里是我的ACF字段代码: if( function_exists('acf_add_local_field_group') ): acf_add_local_field_group(array( 'key' => 'gr

我正在搜索一种在创建新订单时为主题和收件人电子邮件创建新占位符的方法?我已经用ACF创建了新字段:“chantier”和“conducteur de travaux”,我希望能够在我的主题邮件中添加{chantier},并将{conducteur}添加到我的收件人

这里是我的ACF字段代码:

 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;
我已尝试使用此处建议的代码:

但它不起作用,下面是我的代码:

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 );
}
当我收到邮件时,我就收到了:订单{chantier}。 我不知道我的错误在哪里

对于字段“conducteur de travaux”(用户字段),我想恢复“conducteur de travaux”的电子邮件,并将其添加到新订单收件人电子邮件中。可能吗

注意:关于stackOverFlow为避免您的问题因“过于宽泛”而被关闭,请始终提出一个小问题,而不是同时提出多个不同的问题。规则是一个问题的一个答案

您的代码中有一些错误,如未定义的
$order\u id
,因此无法工作

您需要确保用于获取ACF值的meta_键。似乎它应该是
而不是
“\u chantier”
(你可以尝试两者,看看哪一个有效)

下面的代码将为ACF值的电子邮件通知主题添加新的占位符:

add_filter( 'woocommerce_email_format_string', 'add_custom_email_format_string', 10, 2 );
function add_custom_email_format_string( $string, $email ) {

    // HERE the ACF slug to be used to get the value
    $meta_key = 'chantier';
    
    // Get the instance of the WC_Order object
    $order   = $email->object;
    
    // Get ACF value (with the correct Order ID)
    $value = get_field( $meta_key, $order->get_id() );
    
    // Additional subject placeholder
    $new_placeholders = array( '{chantier}' => $value );
    
    // Get the clean replacement value string for "{chantier}" placeholder
    return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}
代码进入活动子主题(或活动主题)的function.php文件。现在应该可以了

相关工作答复: