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 tutoraspire.com FREE
* @sourcecode https://tutoraspire.com/?p=73381
* @author Tutor Aspire
* @testedwith WooCommerce 3.5.1
* @donate $9 https://tutoraspire.com
*/
// -------------------------
// 1. Display Is this a Gift checkbox
add_action( 'woocommerce_before_add_to_cart_quantity', 'tutoraspire_is_this_gift_add_cart', 35 );
function tutoraspire_is_this_gift_add_cart() {
?>
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', 'tutoraspire_translate_shipping_gift' );
}
}
function tutoraspire_translate_shipping_gift( $translated ) {
$translated = str_ireplace( 'Ship to a different address?', 'Who is this Gift For?', $translated );
return $translated;
}