Override recipient from a custom field on Woocommerce customer email notifications
Dealing with digital products, I've added a GIFT function which works fine, but it does not work if the custom GIFT field is not filled in. If the GIFT field is filled in and when the order is set to Complete, the order complete email is sent to the email put in the GIFT field instead of the one entered as the billing email.
Any idea for where I'm going wrong here? I need it to send to the billing email if the GIFT field is not filled in and if the GIFT field is filled in, send only to the email entered in the GIFT field.
Here is the code:
// 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' );
Here is the code that does not work as I need it to:
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;
}
I would appreciate some help with this. Thanks in advance!
php wordpress woocommerce orders email-notifications
add a comment |
Dealing with digital products, I've added a GIFT function which works fine, but it does not work if the custom GIFT field is not filled in. If the GIFT field is filled in and when the order is set to Complete, the order complete email is sent to the email put in the GIFT field instead of the one entered as the billing email.
Any idea for where I'm going wrong here? I need it to send to the billing email if the GIFT field is not filled in and if the GIFT field is filled in, send only to the email entered in the GIFT field.
Here is the code:
// 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' );
Here is the code that does not work as I need it to:
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;
}
I would appreciate some help with this. Thanks in advance!
php wordpress woocommerce orders email-notifications
In your last function, the variable$gift_recipient_address
is not defined so the condition( ! empty( $gift_recipient_address ))
is never true and It should even throw an error normally.
– LoicTheAztec
Nov 15 '18 at 15:48
@LoicTheAztec, makes sense, and I tried adding anif
before the bracket, but that does not work. Any idea on how to make it defined?
– user10551357
Nov 15 '18 at 15:52
what is $gift_recipient_address ? where does it come from?
– LoicTheAztec
Nov 15 '18 at 15:53
@LoicTheAztec, I used it for updating the meta, see here:// 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 ) ); }
– user10551357
Nov 15 '18 at 15:55
add a comment |
Dealing with digital products, I've added a GIFT function which works fine, but it does not work if the custom GIFT field is not filled in. If the GIFT field is filled in and when the order is set to Complete, the order complete email is sent to the email put in the GIFT field instead of the one entered as the billing email.
Any idea for where I'm going wrong here? I need it to send to the billing email if the GIFT field is not filled in and if the GIFT field is filled in, send only to the email entered in the GIFT field.
Here is the code:
// 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' );
Here is the code that does not work as I need it to:
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;
}
I would appreciate some help with this. Thanks in advance!
php wordpress woocommerce orders email-notifications
Dealing with digital products, I've added a GIFT function which works fine, but it does not work if the custom GIFT field is not filled in. If the GIFT field is filled in and when the order is set to Complete, the order complete email is sent to the email put in the GIFT field instead of the one entered as the billing email.
Any idea for where I'm going wrong here? I need it to send to the billing email if the GIFT field is not filled in and if the GIFT field is filled in, send only to the email entered in the GIFT field.
Here is the code:
// 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' );
Here is the code that does not work as I need it to:
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;
}
I would appreciate some help with this. Thanks in advance!
php wordpress woocommerce orders email-notifications
php wordpress woocommerce orders email-notifications
edited Nov 15 '18 at 16:08
LoicTheAztec
92.9k1366106
92.9k1366106
asked Nov 15 '18 at 15:19
user10551357
In your last function, the variable$gift_recipient_address
is not defined so the condition( ! empty( $gift_recipient_address ))
is never true and It should even throw an error normally.
– LoicTheAztec
Nov 15 '18 at 15:48
@LoicTheAztec, makes sense, and I tried adding anif
before the bracket, but that does not work. Any idea on how to make it defined?
– user10551357
Nov 15 '18 at 15:52
what is $gift_recipient_address ? where does it come from?
– LoicTheAztec
Nov 15 '18 at 15:53
@LoicTheAztec, I used it for updating the meta, see here:// 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 ) ); }
– user10551357
Nov 15 '18 at 15:55
add a comment |
In your last function, the variable$gift_recipient_address
is not defined so the condition( ! empty( $gift_recipient_address ))
is never true and It should even throw an error normally.
– LoicTheAztec
Nov 15 '18 at 15:48
@LoicTheAztec, makes sense, and I tried adding anif
before the bracket, but that does not work. Any idea on how to make it defined?
– user10551357
Nov 15 '18 at 15:52
what is $gift_recipient_address ? where does it come from?
– LoicTheAztec
Nov 15 '18 at 15:53
@LoicTheAztec, I used it for updating the meta, see here:// 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 ) ); }
– user10551357
Nov 15 '18 at 15:55
In your last function, the variable
$gift_recipient_address
is not defined so the condition ( ! empty( $gift_recipient_address ))
is never true and It should even throw an error normally.– LoicTheAztec
Nov 15 '18 at 15:48
In your last function, the variable
$gift_recipient_address
is not defined so the condition ( ! empty( $gift_recipient_address ))
is never true and It should even throw an error normally.– LoicTheAztec
Nov 15 '18 at 15:48
@LoicTheAztec, makes sense, and I tried adding an
if
before the bracket, but that does not work. Any idea on how to make it defined?– user10551357
Nov 15 '18 at 15:52
@LoicTheAztec, makes sense, and I tried adding an
if
before the bracket, but that does not work. Any idea on how to make it defined?– user10551357
Nov 15 '18 at 15:52
what is $gift_recipient_address ? where does it come from?
– LoicTheAztec
Nov 15 '18 at 15:53
what is $gift_recipient_address ? where does it come from?
– LoicTheAztec
Nov 15 '18 at 15:53
@LoicTheAztec, I used it for updating the meta, see here:
// 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 ) ); }
– user10551357
Nov 15 '18 at 15:55
@LoicTheAztec, I used it for updating the meta, see here:
// 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 ) ); }
– user10551357
Nov 15 '18 at 15:55
add a comment |
1 Answer
1
active
oldest
votes
There are some mistakes in your two last functions… try the following (that will replace your two last functions):
// 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;
}
Code goes in function.php file of your active child theme (active theme). It should work.
thank you (once again)! that works fine. Just to gotta add an if to the order message function since it's showing "Gift For:" even when blank.
– user10551357
Nov 15 '18 at 16:13
@WooDevil Updated my code too…
– LoicTheAztec
Nov 15 '18 at 16:23
thank you (again) for taking the time and for helping. I truly appreciate it.
– user10551357
Nov 15 '18 at 16:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322575%2foverride-recipient-from-a-custom-field-on-woocommerce-customer-email-notificatio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are some mistakes in your two last functions… try the following (that will replace your two last functions):
// 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;
}
Code goes in function.php file of your active child theme (active theme). It should work.
thank you (once again)! that works fine. Just to gotta add an if to the order message function since it's showing "Gift For:" even when blank.
– user10551357
Nov 15 '18 at 16:13
@WooDevil Updated my code too…
– LoicTheAztec
Nov 15 '18 at 16:23
thank you (again) for taking the time and for helping. I truly appreciate it.
– user10551357
Nov 15 '18 at 16:42
add a comment |
There are some mistakes in your two last functions… try the following (that will replace your two last functions):
// 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;
}
Code goes in function.php file of your active child theme (active theme). It should work.
thank you (once again)! that works fine. Just to gotta add an if to the order message function since it's showing "Gift For:" even when blank.
– user10551357
Nov 15 '18 at 16:13
@WooDevil Updated my code too…
– LoicTheAztec
Nov 15 '18 at 16:23
thank you (again) for taking the time and for helping. I truly appreciate it.
– user10551357
Nov 15 '18 at 16:42
add a comment |
There are some mistakes in your two last functions… try the following (that will replace your two last functions):
// 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;
}
Code goes in function.php file of your active child theme (active theme). It should work.
There are some mistakes in your two last functions… try the following (that will replace your two last functions):
// 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;
}
Code goes in function.php file of your active child theme (active theme). It should work.
edited Nov 15 '18 at 16:22
answered Nov 15 '18 at 16:05
LoicTheAztecLoicTheAztec
92.9k1366106
92.9k1366106
thank you (once again)! that works fine. Just to gotta add an if to the order message function since it's showing "Gift For:" even when blank.
– user10551357
Nov 15 '18 at 16:13
@WooDevil Updated my code too…
– LoicTheAztec
Nov 15 '18 at 16:23
thank you (again) for taking the time and for helping. I truly appreciate it.
– user10551357
Nov 15 '18 at 16:42
add a comment |
thank you (once again)! that works fine. Just to gotta add an if to the order message function since it's showing "Gift For:" even when blank.
– user10551357
Nov 15 '18 at 16:13
@WooDevil Updated my code too…
– LoicTheAztec
Nov 15 '18 at 16:23
thank you (again) for taking the time and for helping. I truly appreciate it.
– user10551357
Nov 15 '18 at 16:42
thank you (once again)! that works fine. Just to gotta add an if to the order message function since it's showing "Gift For:" even when blank.
– user10551357
Nov 15 '18 at 16:13
thank you (once again)! that works fine. Just to gotta add an if to the order message function since it's showing "Gift For:" even when blank.
– user10551357
Nov 15 '18 at 16:13
@WooDevil Updated my code too…
– LoicTheAztec
Nov 15 '18 at 16:23
@WooDevil Updated my code too…
– LoicTheAztec
Nov 15 '18 at 16:23
thank you (again) for taking the time and for helping. I truly appreciate it.
– user10551357
Nov 15 '18 at 16:42
thank you (again) for taking the time and for helping. I truly appreciate it.
– user10551357
Nov 15 '18 at 16:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322575%2foverride-recipient-from-a-custom-field-on-woocommerce-customer-email-notificatio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In your last function, the variable
$gift_recipient_address
is not defined so the condition( ! empty( $gift_recipient_address ))
is never true and It should even throw an error normally.– LoicTheAztec
Nov 15 '18 at 15:48
@LoicTheAztec, makes sense, and I tried adding an
if
before the bracket, but that does not work. Any idea on how to make it defined?– user10551357
Nov 15 '18 at 15:52
what is $gift_recipient_address ? where does it come from?
– LoicTheAztec
Nov 15 '18 at 15:53
@LoicTheAztec, I used it for updating the meta, see here:
// 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 ) ); }
– user10551357
Nov 15 '18 at 15:55