带有 ACF 主题的 Woocommerce 电子邮件通知的其他占位符 [英] Additional placeholders for Woocommerce email notifications subject with ACF

查看:25
本文介绍了带有 ACF 主题的 Woocommerce 电子邮件通知的其他占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的 ACF 字段代码:

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 :

为 Woocommerce 电子邮件通知主题创建其他变量/占位符

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

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 );
}

当我收到邮件时,我收到了:订购 {chantier}.我不知道我的错误在哪里...

When I receive the mail I have it : Order {chantier}. I don't know where is my mistake...

对于字段conducteur-de-travaux"(用户字段),我想恢复conducteur de travaux"的电子邮件并将其添加到新的订单收件人电子邮件中.是否可以?

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?

推荐答案

注意:在 stackOverFlow 上以避免您的问题因过于宽泛"而被关闭;原因,总是问一个小问题而不是一次问多个不同的问题.规则是一个问题的一个答案.

Note: On stackOverFlow to avoid your question to be closed for "too broad" reason, always ask one small question instead of asking multiple distinct questions at once. The rule is one answer to one question.

您的代码中存在一些错误和错误,例如未定义的 $order_id,因此无法运行.

There is some errors and mistakes in your code like $order_id that is not defined, so it can't work.

您需要确定用于获取 ACF 值的 meta_key.看起来应该是'chantier'而不是'_chantier' (你可以尝试两者看看哪个有效).

You need to be sure of the meta_key to be used to get the ACF value. It seem that it should be 'chantier' instead of '_chantier' (You can try both of them to see which one works).

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

The code below will ad new placeholders for email notifications subject for ACF value:

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 文件中.现在应该可以使用了.

Code goes in function.php file of your active child theme (or active theme). It should works now.

相关的工作答案:

这篇关于带有 ACF 主题的 Woocommerce 电子邮件通知的其他占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆