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        Get CustomizeWoo.com FREE
* @sourcecode    https://businessbloomer.com/?p=73381
* @author        Rodolfo Melogli
* @testedwith    WooCommerce 3.5.1
* @community     https://businessbloomer.com/club/
*/

// -------------------------
// 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 custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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. Follow @rmelogli

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

  1. hi, is this snippet configurable to add extra price in gift wrapping? or is just the checkbox? thanks in advance, great work.

    1. Hey Mike, yes, you can totally add another piece of code to add an extra fee, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hi,
    Added the snippet to my site, the checkbox, and text displayed in the product as expected, however, the variable does not get passed through to the ordering and checkout pages.
    Others have commented that the function does not pass over and your response is to “hire me if you want it to work”.
    What is the purpose of creating a snippet if it is not fully functional? These type of tactics doesn’t make you a credible person. And I was going to signup for classes, but after reading some of the comments this makes me think twice.
    You’re going to probably delete this comment anyway.

    1. Sally, thanks a lot for your feedback and sorry for the delay, just had a baby! And no, I don’t delete this kind of comments either.

      So, this snippet is simply something I coded for a client of mine. They didn’t ask for this modification so that’s why you don’t find this feature.

      For many this is enough, for others this is not, so in the blog comments I always tell people that they can hire me if they wish to add some modification, that’s normal trading and you can be assured there is no catch.

      Does that help? If not, please help me help you better with some additional feedback ๐Ÿ™‚

  3. Hi Rodolfo,

    First off, a big thank you for all your great work posting all these woocommerce snippets! I’ve found several that have been very helpful. I am curious however about how you determine what field to reference when adding a filter like the one you’ve added for this snippet. For example how did you know to reference ‘woocommerce_ship_to_different_address_checked” in the code below to check the ship to different address checkbox? There are other fields I would like to hide/display or check/uncheck and can’t figure out how to reference them. Is there some WooCommerce template or class code where these are listed? Or some other method?

     if ( $itsagift == true ) {
          add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true' );
          add_filter( 'gettext', 'bbloomer_translate_shipping_gift' );
       }
    
    1. Hi Rick! There are not many references out there for filters, while there are many for actions (https://www.businessbloomer.com/category/woocommerce-tips/visual-hook-series/ for example). For filters, I simply check through the WooCommerce plugin files with some advanced searches.

  4. This is brilliant thank you. You have been recommended in a Facebook Group. I have added this snippet but the wording doesn’t change on the checkout page. (Shipping to a different addressโ€ into โ€œWho is this gift for?โ€ if a โ€œgiftโ€ is in the cart)… I have added the code into the child theme functions.php as suggested.

    1. Thank you Amanda! What does your checkout wording say?

  5. Hi

    I want to display a receiver’s email field if the checkbox is checked and then I want to send custom email with order details on receivers email also when order is placed.

    is it possible?
    Please provide me solution for this.

    Thanks in advance.

    1. Hi there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Hey, I just added it to my site, and placed a test order. When i go into my dashboard to review the order im not notified anywhere on the order that its a gift. Am i missing something?

    1. True. But anyhow, shipping and billing addresses will be different, so in that case you’ll know it’s very likely a gift. In case you need to have a special note on the order and identify gift orders, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Hi Rodolfo. I’ve implemented your code and it all works flawlessly. However, I’m having one issue. The Gift Note custom field is showing on the checkout page even when the gift checkbox is not checked. And it’s always required. If there’s no data in the field I get the “shiny new field” error.

    1. Hi Paul! My snippet adds a custom field to products, not the checkout – and there is no checkout validation for that. Sure that you’re using my snippet?

  8. Hi Rodolfo. Just implemented your code snippet and it works flawlessly through checkout. I’m impressed it automatically displays the alternate shipping address based on it being a gift. The only thing I’m not seeing is where the gift note shows to an admin after the sale transaction is complete so he/she will know it’s a gift and include a note in the shipment.

    1. You’re right, this is not saved to the order. Take a look at part 5 here and you should figure that out: https://businessbloomer.com/woocommerce-add-custom-checkout-field-php/

      1. Hi Rodolfo. In the link above, there is no part 5. Can you tell me if it’s possible to tag an order as a gift for the admin filling orders? I may just be missing something.Thanks for your help. (Note: I did customize the three steps on the page above, but it passes the field info to the order page whether it’s a gift or not).

        1. Sorry, I meant part 3

  9. Suppose, instead this was an extra service price option checkbox. How to add it to the product price.

    1. Hi Ankish, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  10. I have implemented it to staging version and it worked but I made few changes on the order form. So, I need to update the second form as well.
    I offer Cash on Delivery and Bank Transfer payment options. COD doesn’t make sense with these changes. So, I need to disable the COD when the product item is Gift.

    Thanks, great work.

    1. Muhammad, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

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

    my apologies, but it does work

    1. Great!

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

  13. 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 ๐Ÿ™‚

  14. 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 ๐Ÿ™‚

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

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

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

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

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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