Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kyzoe/12ae462077705c3d400f95158dfff8f1 to your computer and use it in GitHub Desktop.
Save Kyzoe/12ae462077705c3d400f95158dfff8f1 to your computer and use it in GitHub Desktop.
WooCommerce: Is this a gift + Validation + Email + Order Details
// add gift message on checkout after the "Order Notes"
add_action( 'woocommerce_after_order_notes', 'bdev_is_this_a_gift' );
function bdev_is_this_a_gift() {
$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, nothing more. <a href="/privacy-policy" style="text-decoration: underline">More information here.</a>'),
'placeholder' => __('john.doe@example.com'),
), 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
}
// validate the gift field and display error
add_action('woocommerce_after_checkout_validation', 'bdev_gift_validation', 20, 2 );
function bdev_gift_validation( $data, $errors ) {
// valide "Gift" not empty if selected
if( isset($_POST['gift_msg']) && isset($_POST['gift']) && empty($_POST['gift']) )
$errors->add( 'gift', __( "You've chosen to send this product as a gift, but did not submit a recipient email address.", "woocommerce" ) );
// validate "Gift" input email different from billing email
if ( isset($_POST['gift']) && $data['billing_email'] === $_POST['gift'] ) {
$errors->add( 'gift', __( "You cannot send a product as a gift to yourself. That defeats the purpose of this being a gift, does it not?", "woocommerce" ) );
}
}
// update the gift field
add_action( 'woocommerce_checkout_update_order_meta', 'bdev_is_this_a_gift_save');
function bdev_is_this_a_gift_save( $order_id ) {
$gift_recipient_address = $_POST['gift'];
if ( ! empty( $gift_recipient_address ) )
update_post_meta( $order_id, 'gift', sanitize_text_field( $gift_recipient_address ) );
}
// show if gift on the 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;
}
// send to the gift receiver if provided
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'bdev_is_this_a_gift_replace_email_recipient', 10, 2 );
function bdev_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;
}
// add text to gift reciever email
add_action( 'woocommerce_email_before_order_table', 'bdev_this_was_a_gift', 20, 4 );
function bdev_this_was_a_gift( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' && $order->get_meta('gift') == true ) {
echo '<h2 class="gift-h">Happy Days! You Got A GIFT!</h2><p class="gift-p">Your text here followed by "<strong>HAPPYCODE</strong>" and some more text at the end.</p>'; } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment