WooCommercer / WP - 将自定义字段输出添加到管理新订单电子邮件中

时间:2015-06-09 16:12:39

标签: php wordpress email woocommerce

遇到与在woocommerce中的结帐页面上创建新的自定义下拉菜单相关的问题。我能够使用我在这里和那里找到的代码构建自定义字段,但我无法通过我的电子邮件模板向我发送结果(用户选择的内容)。

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

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

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

<?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' ); ?>

我想要做的就是在该电子邮件中添加一行,几乎在任何地方,都会说“自定义字段的输出是choiceX”,这会告诉我用户选择了哪一个。< / p>

我似乎无法找到变量或我必须使用的代码才能使其发挥作用。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

尝试查看我在customizing WooCommerce checkout fields上的教程,其中包括有关向电子邮件添加数据的部分。最简单的方法是将您的数据添加到WooCommerce将显示的订单元数组中:

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

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

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 );
相关问题