1
votes

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!

1
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
@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
what is $gift_recipient_address ? where does it come from?LoicTheAztec
@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

1 Answers

0
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.