Php Woocommerce预订-将自定义字段添加到管理员电子邮件

Php Woocommerce预订-将自定义字段添加到管理员电子邮件,php,wordpress,email,woocommerce,woothemes,Php,Wordpress,Email,Woocommerce,Woothemes,我很难弄清楚如何将新的自定义帐单字段添加到我的管理员通知电子邮件中。我不是php专家(无论如何),所以我不确定我搞砸了什么,或者是因为它试图将该字段附加到通用woo commerce管理员通知电子邮件或来自Bookings插件的电子邮件中。 我的自定义字段在账单页面上运行良好,它只是将其输入到管理员电子邮件中,最好也输入后端管理员订单管理区域 老实说,我在圣诞节前就在做这件事,完全不知道我在做什么,在哪里 提前谢谢你 这是我的代码 /** * Add the field to the chec

我很难弄清楚如何将新的自定义帐单字段添加到我的管理员通知电子邮件中。我不是php专家(无论如何),所以我不确定我搞砸了什么,或者是因为它试图将该字段附加到通用woo commerce管理员通知电子邮件或来自Bookings插件的电子邮件中。 我的自定义字段在账单页面上运行良好,它只是将其输入到管理员电子邮件中,最好也输入后端管理员订单管理区域

老实说,我在圣诞节前就在做这件事,完全不知道我在做什么,在哪里

提前谢谢你

这是我的代码

/**
 * Add the field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>'.__('Referral Source').'</h3>';

    /**
     * Output the field. This is for 1.4.
     *
     * To make it compatible with 1.3 use $checkout->checkout_form_field instead:

     $checkout->checkout_form_field( 'my_field_name', array( 
        'type'          => 'text', 
        'class'         => array('my-field-class orm-row-wide'), 
        'label'         => __('Fill in this field'), 
        'placeholder'   => __('Enter a number'),
        ));
     **/
    woocommerce_form_field( 'my_field_name', array( 
        'type'          => 'text', 
        'class'         => array('my-field-class form-row-wide'), 
        'label'         => __('How did you hear about us?'), 
        'placeholder'   => __('Please enter how you were referred to our clinic.'),
        'required'  => true,
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';

    }

/**
 * Update the user meta with field value
 **/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
    if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}
/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}
/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = 'Referral Source';
    return $keys;
}

此方法已在WooCommerce 2.3中更新,但它应该与您的方法向后兼容。您在
woocommerce\u email\u order\u meta\u keys
筛选器上添加到数组中的密钥似乎与您已更新的meta密钥的名称不匹配。请返回基本数组。就像您拥有它一样,但是您的密钥与您在
my\u custom\u checkout\u字段\u update\u order\u meta()
函数中更新的元密钥不匹配

以下是如何在WC 2.3+中向电子邮件添加密钥:

   // WooCommerce 2.3+
    function kia_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
        $fields['_some_field'] = array(
                    'label' => __( 'Some field' ),
                    'value' => get_post_meta( $order->id, '_some_field', true ),
                );
        $fields['_another_field'] = array(
                    'label' => __( 'Another field' ),
                    'value' => get_post_meta( $order->id, '_another_field', true ),
                );
        return $fields;
    }
    add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_fields', 10, 3 );

这是否也适用于woo Bookings插件驱动的订单电子邮件?基于该示例,这是否正确?很抱歉,我的php不是很好,()它能工作吗?这是最终的答案。如果它没有达到你的目标,那么你可以编辑你的问题来粘贴你正在尝试的新代码,或者将问题提交一个新问题。代码的屏幕截图很难使用。如果你不告诉我们哪一部分失败了,那就很难调查了。虽然从一个简单的一瞥来看,我认为你需要重新审视。您在
update\u post\u meta
函数中使用的
$meta\u键与在
get\u post\u meta
函数中使用的
$meta\u键不同。很抱歉,我在这里是新手。我尝试在我的示例中发布代码,但无法使代码的迷你标记格式正常工作。不,不幸的是,代码对我不起作用,我相信你是对的,我出了点问题。我对php的了解还不够,甚至连阅读wordpress文档都弄不明白。不过,感谢您的帮助,如果我对其进行排序,我将重新发布您没有保存任何名为
参考源的字段,因此,没有显示任何内容。我有一个关于这个的教程,它将引导你完成整个过程。
   // WooCommerce 2.3+
    function kia_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
        $fields['_some_field'] = array(
                    'label' => __( 'Some field' ),
                    'value' => get_post_meta( $order->id, '_some_field', true ),
                );
        $fields['_another_field'] = array(
                    'label' => __( 'Another field' ),
                    'value' => get_post_meta( $order->id, '_another_field', true ),
                );
        return $fields;
    }
    add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_fields', 10, 3 );