Skip to content

Instantly share code, notes, and snippets.

@woogists
Created February 27, 2025 20:34
Show Gist options
  • Save woogists/70e4a6e0a82424611f17429bc593f4dc to your computer and use it in GitHub Desktop.
Save woogists/70e4a6e0a82424611f17429bc593f4dc to your computer and use it in GitHub Desktop.
an example snippet of how to add a custom field to the Gift Cards by WooCommerce extension
<?php
/*
Below, is an example snippet of how to add a "Recipient Name" field to the Gift Cards by WooCommerce extension
Adding a new field in this form is a technically demanding task, as it must:
- be displayed in the single product page,
- be validated when the Add to Cart button is clicked,
- be included in the Gift Cards Importer/Exporter and;
- be included as a placeholder in WooCommerce > Settings > Emails > Gift Card received.
*/
// 1. Add a name field.
add_filter( 'woocommerce_gc_form_fields', 'sw_gc_add_giftcard_to_name_field' );
function sw_gc_add_giftcard_to_name_field( $form_fields ) {
$form_fields[ 'wc_gc_giftcard_to_name' ] = 'To (Name)';
return $form_fields;
}
// 2. Print field html.
add_filter( 'woocommerce_gc_form_fields_html', 'sw_gc_add_giftcard_to_name_field_html', 15 );
function sw_gc_add_giftcard_to_name_field_html( $product ) {
$value = isset( $_REQUEST[ 'wc_gc_giftcard_to_name' ] ) ? sanitize_text_field( wp_unslash( wptexturize( $_REQUEST[ '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" name="wc_gc_giftcard_to_name" placeholder="<?php echo sprintf( esc_attr__( 'Enter recipient\'s name', 'woocommerce-gift-cards' ), wc_gc_get_emails_delimiter() ); ?>" value="<?php echo $value; ?>"/>
</div>
<?php
}
// 3. Validate custom field value (Optional)
add_action( 'woocommerce_gc_validate_form_fields', 'sw_gc_validate_giftcard_to_name_field' );
function sw_gc_validate_giftcard_to_name_field( $configuration ) {
if ( empty( $configuration[ 'wc_gc_giftcard_to_name' ] ) ) {
throw new Exception( 'Invalid Giftcard To Name' );
}
}
// 4. Include in export CSV.
add_filter( 'woocommerce_gc_giftcards_export_default_columns', 'sw_gc_export_add_giftcard_to_name_field' );
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_recipient_name', 'sw_gc_export_process_giftcard_to_name_field', 10, 2 );
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 ) {
// Do nothing...
}
return $value;
}
// 5. Display the new Email PlaceHolder in the email tooltips in WooCommerce > Settings > Emails > Gift Card received
// Filter is available from version 1.7.x
add_filter( 'woocommerce_gc_email_placeholders', 'sw_gc_email_placeholders', 10 );
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
// Filter is available from version 1.7.x
add_filter( 'woocommerce_gc_email_placeholder_giftcard_to_name_value', 'sw_gc_email_placeholders_giftcard_to_name_field', 10, 2 );
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 ) {
// Do nothing...
return $value;
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment