从Woocommerce客户电子邮件通知上的自定义字段覆盖收件人

时间:2018-11-15 15:19:37

标签: php wordpress woocommerce orders email-notifications

处理数字产品时,我添加了一个GIFT函数,该函数可以正常工作,但是如果未填写自定义GIFT字段,该函数将不起作用。如果GIFT字段已填写,并且订单设置为“完成”,订单完成电子邮件将发送到GIFT字段中的电子邮件,而不是输入为计费电子邮件的电子邮件。

有什么想法我要在这里出错吗?如果没有填写GIFT字段,并且如果填写了GIFT字段,我需要将其发送到帐单电子邮件,仅发送到在GIFT字段中输入的电子邮件。

代码如下:

// add gift message on checkout
add_action( 'woocommerce_after_order_notes', 'custom_checkout_field_before_billing' );
function custom_checkout_field_before_billing() {
    $domain = 'woocommerce';

    ?>
    <style>p#gift_field{display:none;}</style>
    <div id="message">
    <h3><i class="fa fa-gift"></i><?php _e( ' Is this a gift?', 'woocommerce' ); ?></h3>
    <?php

    woocommerce_form_field( 'gift_msg', array(
        'type'  => 'checkbox',
        'class' => array( 'gift-checkbox' ),
        'label' => __( 'To whom is this a gift?', 'woocommerce' ),
    ), WC()->checkout->get_value( 'cb_msg' ));

    woocommerce_form_field( 'gift', array(
        'type'              => 'text',
        'class'             => array('msg t_msg'),
        'label'             => __('Enter the recipient\'s e-mail address, e.g: john.smith@email.com '),
        'placeholder'       => __(''),
    ), WC()->checkout->get_value( 'gift' ));

    echo '</div>';

    ?><script>
    jQuery(document).ready(function($) {
        var a = '#gift_field';
        $('input#gift_msg').change( function(){
            if( $(this).is(':checked') )
                $(a).show();
            else
                $(a).hide();
        });
    });
    </script><?php
}

// add validation if box is checked but field is not filled in
add_action('woocommerce_after_checkout_validation', 'is_this_a_gift_validation', 20, 2 );
function is_this_a_gift_validation( $data, $errors ) {

    if( isset($_POST['gift_msg']) && empty($_POST['gift']) )
        $errors->add( 'gift', __( "You've chosen to send this as a gift, but did not submit a recipient email address.", "woocommerce" ) );
}



// update the gift field meta
add_action( 'woocommerce_checkout_update_order_meta', 'is_this_a_gift_save_meta');
function is_this_a_gift_save_meta( $order_id ) {
    $gift_recipient_address = $_POST['gift'];
    if ( ! empty( $gift_recipient_address ) )
        update_post_meta( $order_id, 'gift', sanitize_text_field( $gift_recipient_address ) );
}


// add gift message to order page
function is_this_a_gift_order_display( $order ) {  ?>
    <div class="order_data_column">
        <h3><?php _e( '<br>Gift For:', 'woocommerce' ); ?></h3>
        <?php 
        echo get_post_meta( $order->id, 'gift', true ); ?>
    </div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'is_this_a_gift_order_display' );

以下是我无法使用的代码:

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'is_this_a_gift_replace_email_recipient', 10, 2 );
function is_this_a_gift_replace_email_recipient( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) &&  ( ! empty( $gift_recipient_address ))) return $recipient;

    $recipient = get_post_meta( $order->id, 'gift', true );
    return $recipient;
}

我将对此有所帮助。预先感谢!

1 个答案:

答案 0 :(得分:0)

您的后两个功能有一些错误……请尝试以下(它将替换您的后两个功能)

// add gift message to order page
add_action( 'woocommerce_admin_order_data_after_order_details', 'is_this_a_gift_order_display' );
function is_this_a_gift_order_display( $order ) {  

    if( $value = $order->get_meta('gift') ) :
        echo '<div class="order_data_column">
        <h3>' . __( '<br>Gift For:', 'woocommerce' ) . '</h3>
        <p>' . $value . '</p>
        </div>';
    endif;
}

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'is_this_a_gift_replace_email_recipient', 10, 2 );
function is_this_a_gift_replace_email_recipient( $recipient, $order ) {
    $gift_recipient = $order->get_meta('gift');
    if ( is_a( $order, 'WC_Order' ) && $order->get_meta('gift') ) 
        $recipient = $order->get_meta('gift');

    return $recipient;
}

代码进入您的活动子主题(活动主题)的function.php文件中。应该可以。