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: [email protected] '),
'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!
$gift_recipient_address
is not defined so the condition( ! empty( $gift_recipient_address ))
is never true and It should even throw an error normally. – LoicTheAztecif
before the bracket, but that does not work. Any idea on how to make it defined? – user10551357// 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