Add a recipient name field to gift cards

Add a recipient name field to the gift card product form and make it available in exports and email placeholders. Importing this custom field requires additional code.

Note:

This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.

Test custom code on a staging site before using it on your store. Add PHP to a custom plugin or a child theme’s functions.php file. If the file is already inside a PHP block, omit the opening PHP tag.

This example adds a required Recipient Name field to the gift card form. It adds the name to an export column and makes {giftcard_to_name} available in Gift Card received email templates for gift cards linked to an order item. Importing the custom field requires additional code.

<?php
add_filter( 'woocommerce_gc_form_fields', 'sw_gc_add_giftcard_to_name_field' );
/**
 * Register the recipient name field.
 *
 * @param array $form_fields Existing fields.
 * @return array
 */
function sw_gc_add_giftcard_to_name_field( $form_fields ) {
	$form_fields['wc_gc_giftcard_to_name'] = 'To (Name)';
	return $form_fields;
}
// Sanitize the field before Gift Cards stores it with the cart item.
add_filter( 'woocommerce_gc_parse_custom_form_field', 'sw_gc_sanitize_recipient_name', 10, 2 );
/**
 * Sanitize the submitted recipient name.
 *
 * @param mixed  $value Submitted value.
 * @param string $key Field key.
 * @return mixed
 */
function sw_gc_sanitize_recipient_name( $value, $key ) {
	if ( 'wc_gc_giftcard_to_name' !== $key ) {
		return $value;
	}
	return is_scalar( $value ) ? sanitize_text_field( wp_unslash( (string) $value ) ) : '';
}

// 2. Print field HTML.
add_action( 'woocommerce_gc_form_fields_html', 'sw_gc_add_giftcard_to_name_field_html', 15 );
/**
 * Display the recipient name field.
 */
function sw_gc_add_giftcard_to_name_field_html() {
	$value = sanitize_text_field( WC_GC_Gift_Card_Product::get_form_field_value( 'wc_gc_giftcard_to_name' ) );
	?>
	<div class="wc_gc_field wc_gc_giftcard_to_name">
		<label for="wc_gc_giftcard_to_name"><?php esc_html_e( 'To (Name)', 'woocommerce-gift-cards' ); ?></label>
		<input type="text" id="wc_gc_giftcard_to_name" name="wc_gc_giftcard_to_name" placeholder="<?php esc_attr_e( 'Enter recipient\'s name', 'woocommerce-gift-cards' ); ?>" value="<?php echo esc_attr( $value ); ?>"/>
	</div>
	<?php
}
// 3. Require a recipient name.
add_action( 'woocommerce_gc_validate_form_fields', 'sw_gc_validate_giftcard_to_name_field' );
/**
 * Require a recipient name.
 *
 * @param array $configuration Gift card fields.
 * @throws Exception If the name is empty.
 */
function sw_gc_validate_giftcard_to_name_field( $configuration ) {
	if ( empty( $configuration['wc_gc_giftcard_to_name'] ) ) {
		throw new Exception( 'Enter a recipient name.' );
	}
}
// 4. Include in export CSV.
add_filter( 'woocommerce_gc_giftcards_export_default_columns', 'sw_gc_export_add_giftcard_to_name_field' );
/**
 * Add the recipient name export column.
 *
 * @param array $columns Existing columns.
 * @return array
 */
function sw_gc_export_add_giftcard_to_name_field( $columns ) {
	$columns['giftcard_to_name'] = __( 'To Name', 'woocommerce-gift-cards' );
	return $columns;
}
add_filter( 'woocommerce_gc_giftcards_export_column_giftcard_to_name', 'sw_gc_export_process_giftcard_to_name_field', 10, 2 );
/**
 * Read the name for the export column.
 *
 * @param mixed           $value Existing value.
 * @param WC_GC_Gift_Card $giftcard Gift card.
 * @return mixed
 */
function sw_gc_export_process_giftcard_to_name_field( $value, $giftcard ) {
	if ( ! $giftcard->get_order_id() || ! $giftcard->get_order_item_id() ) {
		return $value;
	}
	try {
		$order_item = new WC_Order_Item_Product( $giftcard->get_order_item_id() );
		if ( ! $order_item->get_id() ) {
			return $value;
		}
		$value = $order_item->get_meta( 'wc_gc_giftcard_to_name', true );
	} catch ( Exception $e ) {
		return $value;
	}
	return $value;
}




// 5. Display the new Email PlaceHolder in the email tooltips in WooCommerce > Settings > Emails > Gift Card received
add_filter( 'woocommerce_gc_email_placeholders', 'sw_gc_email_placeholders', 10 );
/**
 * Register the recipient name email placeholder.
 *
 * @param array $placeholders Existing placeholders.
 * @return array
 */
function sw_gc_email_placeholders( $placeholders ) {

	$placeholders[] = 'giftcard_to_name';

	return $placeholders;
}


// 6. Hooks into the dynamic filter woocommerce_gc_email_placeholder_`placeholder_name`_value and replaces
// placeholder_name with whatever value we want. In the snippet below, it replaces {giftcard_to_name} with the
// order item meta wc_gc_giftcard_to_name
add_filter( 'woocommerce_gc_email_placeholder_giftcard_to_name_value', 'sw_gc_email_placeholders_giftcard_to_name_field', 10, 2 );
/**
 * Provide the recipient name for email templates.
 *
 * @param mixed           $value Existing value.
 * @param WC_GC_Gift_Card $giftcard Gift card.
 * @return mixed
 */
function sw_gc_email_placeholders_giftcard_to_name_field( $value, $giftcard ) {
	if ( ! $giftcard->get_order_id() || ! $giftcard->get_order_item_id() ) {
		return $value;
	}

	try {
		$order_item = new WC_Order_Item_Product( $giftcard->get_order_item_id() );
		if ( ! $order_item->get_id() ) {
			return $value;
		}
		$meta = $order_item->get_meta( 'wc_gc_giftcard_to_name', true );
		if ( ! empty( $meta ) ) {
			$value = $meta;
		}
	} catch ( Exception $e ) {
		return $value;
	}
	return $value;
}

Verify the change

↑ Back to top

On a staging site, confirm the form requires a recipient name. Place a test order, then check that the name appears in the gift card export and in an email template that uses {giftcard_to_name}. Importing that field requires additional code.

Questions and support

↑ Back to top

Something missing from this documentation? Still have questions and need assistance?

  • If you have a question about a specific extension or theme you’d like to purchase, contact us to get answers.
  • If you already purchased this product and need some assistance, get in touch with a Happiness Engineer via our support page and select this product’s name from the Product dropdown.

Related Products

Offer Royal Mail shipping rates to your customers.

Notify customers when your products are restocked.

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.