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 and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

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

  1. I wrote a comment a few mins ago, saying this snippet doesnt work

    my apologies, but it does work

    1. Great!

  2. I just tried this and it didn’t work for me. Is this still working?

  3. Hi Rodolfo,

    I came across this and its exactly the function I needed yet it just didn’t work with Woocommerce subscriptions and variations so I went I bought an add-on, Gifting for Woocommerce Subscriptions, but it doesn’t check the shipping address by default like your function above, is there any way I can use part 4. of your function with my Gifting plugin?

    1. Hello Chris, thanks so much for your comment! Yes, this is possible – I think. Try to use part 4 and see if you manage to make it work ๐Ÿ™‚

  4. Hi There,
    I love this snippet but I have one problem. The majority of my products this works great for but I have a handful of products that I don’t want this to show as an option, I have tried different things and can’t get it to work to exclude this checkbox on those products.

    Could you lead me into the right direction.

    1. Hello Steven, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

  5. hi there, i have just tried the code but it doesn’t work here, i can select this is a gift but what it does is select automatically delivery to a different address. Thanks.

    1. Correct Luca, that’s what id does – the gift recipient usually has a different shipping address than the person buying it. Maybe the part that’s not working for you is the translation bit, in case you use WooCommerce in a different language – you’ll need to fix that part ๐Ÿ™‚ Hope this helps

  6. 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/

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

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