从 Woocommerce 客户电子邮件通知的自定义字段覆盖收件人
Override recipient from a custom field on Woocommerce customer 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;
}
我将不胜感激。提前致谢!
你的最后两个函数有一些错误…试试下面的(这将取代你的最后两个函数):
// 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 文件。应该可以。
处理数码产品,我添加了一个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;
}
我将不胜感激。提前致谢!
你的最后两个函数有一些错误…试试下面的(这将取代你的最后两个函数):
// 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 文件。应该可以。