【问题标题】:Override recipient from a custom field on Woocommerce customer email notifications从 Woocommerce 客户电子邮件通知的自定义字段中覆盖收件人
【发布时间】:2018-11-15 15:19:36
【问题描述】:

处理数码产品,我添加了一个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;
}

我将不胜感激。提前致谢!

【问题讨论】:

  • 在您的最后一个函数中,变量$gift_recipient_address 没有定义,所以条件( ! empty( $gift_recipient_address )) 永远不会为真,它甚至应该正常抛出错误。
  • @LoicTheAztec,有道理,我尝试在括号前添加if,但这不起作用。关于如何定义它的任何想法?
  • 什么是 $gift_recipient_address ?它来自哪里?
  • @LoicTheAztec,我用它来更新元数据,见这里:// 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 ) ); }

标签: php wordpress woocommerce orders email-notifications


【解决方案1】:

你的最后两个函数有一些错误……试试下面的(这将取代你最后两个函数)

// 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 文件中。它应该可以工作。

【讨论】:

  • 谢谢(再次)!效果很好。只是要在订单消息功能中添加一个 if,因为它显示“Gift For:”,即使是空白。
  • 再次感谢您抽出宝贵的时间并提供帮助。我真的很感激。
猜你喜欢
  • 2022-01-26
  • 2017-05-19
  • 2018-02-25
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2018-01-07
相关资源
最近更新 更多