WooCommercer / WP - 将自定义字段输出添加到管理新订单电子邮件 [英] WooCommercer/WP - Add custom field output to admin-new-order email

查看:222
本文介绍了WooCommercer / WP - 将自定义字段输出添加到管理新订单电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的结帐页面上产生与自动下载菜单相关的问题。我可以使用我在这里和那里发现的代码来构建自定义字段,但是我无法通过我的电子邮件模板向我发送结果(用户选择的内容)。



这是我添加到我的function.php以创建新字段的代码。这是一个有3个选项的下拉菜单,用户可以选择一个。

  / ** 
*将字段添加到结帐
** /
add_action('woocommerce_after_order_notes','my_custom_checkout_field');

函数my_custom_checkout_field($ checkout){

echo'< div id =my_custom_checkout_field>< h3>'__('Méthodede Paiement')。 '< / H3>';

woocommerce_form_field('my_field_name',array(
'type'=>'select',
'class'=> array('my-field-class form-行),
'required'=> true,
'label'=> __('Faites votre choix de paiement'),
'placeholder'=> __ ('Enter something'),
'options'=> array(
'choice1'=> __('Cash - Montant exact','woocommerce'),
'choice2' ='_'('Cash - Montant inexact','woocommerce'),
'choice3'=> __('Carte','woocommerce')

),$ checkout - > get_value('my_field_name'));

echo'< / div>';

}

/ **
*处理结帐
** /
add_action('woocommerce_checkout_process','my_custom_checkout_field_process');

函数my_custom_checkout_field_process(){
global $ woocommerce;

//检查是否设置,如果未设置则添加错误。
if(!$ _ POST ['my_field_name'])
$ woocommerce-> add_error(__('Ce Champ est obligatoire,veuillez choisir。
}

/ **
*更新字段值
** /
add_action('woocommerce_checkout_update_order_meta','my_custom_checkout_field_update_order_meta')的顺序元素;

函数my_custom_checkout_field_update_order_meta($ order_id){
if($ _POST ['my_field_name'])update_post_meta($ order_id,'我的字段',esc_attr($ _ POST ['my_field_name'])) ;
}

现在这是admin-new-order.php中的代码,是发送新订单时,woocommerce网站的管理员获得的电子邮件模板。

 <?php 
/ **
*管理新订单电子邮件
*
* @author WooThemes
* @package WooCommerce /模板/电子邮件/ HTML
* @version 2.0.0
* /

if(!defined('ABSPATH')){
exit; //直接访问退出
}

?>

<?php do_action('woocommerce_email_header',$ email_heading); ?>

< p><?php printf(__('您已从%s收到订单,顺序如下:','woocommerce'),$ order-> billing_first_name。 ''。$ order-> billing_last_name); ?>< / p为H.

<?php do_action('woocommerce_email_before_order_table',$ order,true,false); ?>

< h2< a href =<?php echo admin_url('post.php?post ='。$ order-> id。& action = edit'); ?><?php printf(__('Order#%s','woocommerce'),$ order-> get_order_number()); ?>< / A> (<?php printf('< time datetime =%s>%s< / time>',date_i18n('c',strtotime($ order-> order_date)),date_i18n(wc_date_format strtotime($ order-> order_date)));?>)< / h2>

< table cellspacing =0cellpadding =6style =width:100%; border:1px solid #eee; border =1bordercolor =#eee>
< thead>
< tr>
< th scope =colstyle =text-align:left; border:1px solid #eee><?php _e('Product','woocommerce'); ?>< /第>
< th scope =colstyle =text-align:left; border:1px solid #eee><?php _e('Quantity','woocommerce'); ?>< /第>
< th scope =colstyle =text-align:left; border:1px solid #eee><?php _e('Price','woocommerce'); ?>< /第>
< / tr>
< / thead>
< tbody>
<?php echo $ order-> email_order_items_table(false,true); ?>
< / tbody>
< tfoot>
<?php
if($ totals = $ order-> get_order_item_totals()){
$ i = 0;
foreach($ totals as $ total){
$ i ++;
?>< tr>
< th scope =rowcolspan =2style =text-align:left; border:1px solid #eee;<?php if($ i == 1)echo'border-top -width:4px;';?>><?php echo $ total ['label']; ?>< /第>
< td style =text-align:left; border:1px solid #eee;<?php if($ i == 1)echo'border-top-width:4px;';?> ><?php echo $ total ['value']; ?>< / TD>
< / tr><?php
}
}
?>
< / tfoot>
< / table>

<?php do_action('woocommerce_email_after_order_table',$ order,true,false); ?>

<?php do_action('woocommerce_email_order_meta',$ order,true,false); ?>

<?php do_action('woocommerce_email_customer_details',$ order,$ sent_to_admin,$ plain_text); ?>

<?php do_action('woocommerce_email_footer'); ?>

我想做的是在电子邮件中添加一行,几乎任何地方, 自定义字段的输出是choiceX,它会告诉我用户选择了哪一个。



我似乎找不到变量或我必须使用这个代码来使这项工作。



任何帮助都将非常感谢。

解决方案

请尝试查看有关定制WooCommerce结帐字段的教程,其中包括关于将数据添加到电子邮件的部分。最简单的方法是将数据添加到WooCommerce将显示的数组元素中:

 函数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),
);
return $ fields;
}
add_filter('woocommerce_email_order_meta_fields','kia_email_order_meta_keys',10,3);

或者您可以通过向其中一个可用的钩子添加内容来自定义输出电子邮件模板(任何说 do_action()是一个钩子):

  $ some_field = get_post_meta($ order-> id,'_some_field',true),
$ another_field = get_post_meta > id,'_another_field',true),
if($ plain_text){
echo'某些字段的值为'。 $ some_field。 '而另一个字段的值为'。 $ another_field;
} else {
echo'< p>< strong>某些字段的值< / strong>是'。 $ some_field。 ',而< strong>另一个字段< / strong>的值是'。 $ another_field;< / P取代;
}
}
add_action('woocommerce_email_customer_details','kia_display_email_order_meta',30,3);


Having an issue related to creating a new custom dropdown menu on my checkout page in woocommerce. I was able to build the custom field using code I found here and there, but I can't get it to actually send me the result (what the user has chosen) via my email template.

Here's the code I added to my function.php in order to create the new field. It's a dropdown menu with 3 choices, user HAVE to select one.

/**
 * 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>'.__('Méthode de Paiement').'</h3>';

    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'select',
        'class'         => array('my-field-class form-row-wide'),
    'required'      => true,
        'label'         => __('Faites votre choix de paiement'),
        'placeholder'       => __('Enter something'),
    'options'     => array(
        'choice1' => __('Cash - Montant exact', 'woocommerce' ),
        'choice2' => __('Cash - Montant inexact', 'woocommerce' ),
        'choice3' => __('Carte', 'woocommerce' )
        )
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';

}

/**
 * Process the checkout
 **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if (!$_POST['my_field_name'])
         $woocommerce->add_error( __('Ce champ est obligatoire, veuillez choisir.') );
}

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

Now this is the code in the admin-new-order.php, which is the email template that the admin of the woocommerce site gets when a new order is sent.

<?php
/**
 * Admin new order email
 *
 * @author WooThemes
 * @package WooCommerce/Templates/Emails/HTML
 * @version 2.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

?>

<?php do_action( 'woocommerce_email_header', $email_heading ); ?>

<p><?php printf( __( 'You have received an order from %s. The order is as follows:', 'woocommerce' ), $order->billing_first_name . ' ' . $order->billing_last_name ); ?></p>

<?php do_action( 'woocommerce_email_before_order_table', $order, true, false ); ?>

<h2><a href="<?php echo admin_url( 'post.php?post=' . $order->id . '&action=edit' ); ?>"><?php printf( __( 'Order #%s', 'woocommerce'), $order->get_order_number() ); ?></a> (<?php printf( '<time datetime="%s">%s</time>', date_i18n( 'c', strtotime( $order->order_date ) ), date_i18n( wc_date_format(), strtotime( $order->order_date ) ) ); ?>)</h2>

<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">
    <thead>
        <tr>
            <th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>
            <th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
            <th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php echo $order->email_order_items_table( false, true ); ?>
    </tbody>
    <tfoot>
        <?php
            if ( $totals = $order->get_order_item_totals() ) {
                $i = 0;
                foreach ( $totals as $total ) {
                    $i++;
                    ?><tr>
                        <th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
                        <td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
                    </tr><?php
                }
            }
        ?>
    </tfoot>
</table>

<?php do_action( 'woocommerce_email_after_order_table', $order, true, false ); ?>

<?php do_action( 'woocommerce_email_order_meta', $order, true, false ); ?>

<?php do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text ); ?>

<?php do_action( 'woocommerce_email_footer' ); ?>

All I want to do is add one line to that email, pretty much anywhere, that says something like "the output for the custom field is choiceX", which would tell me which one of the 3 the user has selected.

I can't seem to find the variable or the code I have to use to make this work.

Any help would be very much appreciated.

解决方案

Try reviewing my tutorial on customizing WooCommerce checkout fields which includes a section on adding the data to emails. The easiest is to just add your data to the array of order meta that WooCommerce will display:

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 ),
            );
    return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_keys', 10, 3 );

Or you can customize the output however you'd like by adding something to one of the available hooks in the email template (anything that says do_action() is a hook):

function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
    $some_field = get_post_meta( $order->id, '_some_field', true ),
    $another_field = get_post_meta( $order->id, '_another_field', true ),
    if( $plain_text ){
        echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
    } else {
        echo '<p>The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field;</p>;
    }
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );

这篇关于WooCommercer / WP - 将自定义字段输出添加到管理新订单电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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