WooCommerce: “Is This a Gift?” Checkbox @ Single Product Page

A client of mine asked me to code a checkbox on the single product page called “Is this a gift?”. They noticed that their customers who want to gift the product to a friend get confused with the “Shipping to a different address” form in the WooCommerce checkout.

So, what about renaming “Shipping to a different address” into “Who is this gift for?” if a “gift” is in the cart? Well, this snippet does just that and you can adapt it / customize it to your specific case.

“Is this a gift?” checkbox @ WooCommerce single product page

PHP Snippet: “Is This a Gift?” Checkbox @ Single Product Page – WooCommerce


/**
* @snippet       "Is This a Gift?" Checkbox @ Single Product Page - WooCommerce
* @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode    https://businessbloomer.com/?p=73381
* @author        Rodolfo Melogli
* @testedwith    WooCommerce 3.5.1
* @donate $9     https://businessbloomer.com/bloomer-armada/
*/

// -------------------------
// 1. Display Is this a Gift checkbox 
 
add_action( 'woocommerce_before_add_to_cart_quantity', 'bbloomer_is_this_gift_add_cart', 35 );
 
function bbloomer_is_this_gift_add_cart() {
		
		?>

		<p class="">
		<label class="checkbox">
		<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="is-gift" id="is-gift" value="Yes"><span>Is this a gift?</span>
		</label>
		</p>

		<?php

}
 
// -------------------------
// 2. Add the custom field to $cart_item
 
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_store_gift', 10, 2 );
 
function bbloomer_store_gift( $cart_item, $product_id ) {
if( isset( $_POST['is-gift'] ) && $_POST['is-gift'] == "Yes" ) {
	$cart_item['is-gift'] = $_POST['is-gift'];
}
return $cart_item; 
}
 
// -------------------------
// 3. Preserve the custom field in the session
 
add_filter( 'woocommerce_get_cart_item_from_session', 'bbloomer_get_cart_items_from_session', 10, 2 );
 
function bbloomer_get_cart_items_from_session( $cart_item, $values ) {
if ( isset( $values['is-gift'] ) ){
	$cart_item['is-gift'] = $values['is-gift'];
}
return $cart_item;
}
 
// -------------------------
// 4. If gift in cart, edit checkout behavior:
// a) open shipping by default
// b) rename shipping title
 
add_action ( 'woocommerce_before_checkout_form', 'bbloomer_gift_checkout' );

function bbloomer_gift_checkout() {
	
	$itsagift = false;
	
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		if ( isset( $cart_item['is-gift'] ) ) {
			$itsagift = true;
			break;
		}
	}
	
	if ( $itsagift == true ) {
		add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true' );
		add_filter( 'gettext', 'bbloomer_translate_shipping_gift' );
	}
	
}

function bbloomer_translate_shipping_gift( $translated ) {
$translated = str_ireplace( 'Ship to a different address?', 'Who is this Gift For?', $translated );
return $translated;
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

6 thoughts on “WooCommerce: “Is This a Gift?” Checkbox @ Single Product Page

  1. This is a brilliant idea, and was really hoping to utilize it. Unfortunately when I add this snippet to my functions.php in my child theme as described above, the entire site is crashed and won’t load. So I tried putting it in my style.css file, and no change was observed. Is this happening for anyone else?

    1. Hey Mike, I just retested it and it works. It goes into functions.php. Try to debug your PHP errors here: https://businessbloomer.com/woocommerce-troubleshooting-mistakes-to-avoid/

  2. Thanks Rodolfo, it’s a great snippet and I’ve also integrated it with the “add fee to cart” snippet you mentioned. All is ok. The only one thing to add to be perfect concern to cart page. In effect, when I select “Is this a gift?” checkbox on product page and then I go to cart page, we have no more way of remove the option if I change my mind. It would be cool adding a checkbox option just below the product(s) in cart page so the customer can decide if hold the gift option or remove it. Is it possible? and…how? Thanks again and..sorry for my English. Greetings from Italy.

    1. Fabrizio, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  3. This would be perfect if you could set a price for gift wrapping too.

Questions? Feedback? Support? Leave your Comment Now!
_____
If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)
¯¯¯¯¯

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.