Use these snippets to customize the appearance and functionality of WooCommerce Gift Cards.
To use a PHP snippet, download the linked file and activate it as you would with any other plugin. Alternatively, copy the contained code into your child theme’s functions.php file.
Scope of support:
We are unable to provide support for customizations under our Support Policy. If you need to customize a snippet or extend its functionality, we recommend working with a Woo Agency Partner or a WooCommerce developer on Codeable.
Snippets
↑ Back to topSet a fixed gift card delivery date/time
↑ Back to topScheduled Gift Cards are delivered around the same time as the time they were added to the cart. For example, a gift card purchased on November 15, 14:00, and scheduled to be delivered on December 1, will be delivered on December 1st around 14.00.
Use the following snippet to set a custom time (hour, minute) for delivering all gift cards scheduled for delivery on a given date:
Remove the “Have a gift card?” form from the cart and checkout pages
↑ Back to topUse the following snippet:
If needed, you can use the existing coupon code form input to apply gift card codes, as well. This functionality is available as a free feature plugin.
Remove the date picker from gift card product forms
↑ Back to topThe delivery date field of the datepicker is set to Now by default. To prevent customers from choosing a custom delivery date, use this snippet.
Prevent customers from applying coupons when paying with gift cards
↑ Back to topUse the following snippet to prevent customers from applying coupons when using gift cards to pay for an order:
Display the remaining balance of every applied gift card
↑ Back to topUse the following snippet to display the remaining balance of any applied gift cards on the cart/checkout page:
Set a specific expiration date for all gift cards created on your store
↑ Back to topTo set a specific expiration date for a gift card, use the following snippet:
Before using this snippet, it is necessary to replace the values in $base_datetime->setDate( '2021', '10', '10' );
 with the custom expiration year/month/date you want to set.
Render the Gift Cards tab on a custom My Account page
↑ Back to topTo render the Gift Cards tab on a custom My Account page, use the following shortcode:
[woocommerce_my_account_giftcards]
Disable Gift Cards account redeeming functionality
↑ Back to topTo remove the:
- Gift Cards section under the My account page, and;
- the Add to Account text in Gift Cards e-mails;
use the following snippet:
add_filter( 'woocommerce_gc_is_redeeming_enabled', '__return_false' );
Remove default style rules from the Delivery Date field
↑ Back to topT​o completely remove the default style rules from the Delivery Date field and avoid conflict with theme rules, use the following snippet:
add_filter( 'woocommerce_gc_disable_datepicker_styles', '__return_true' );
N​ote: This customization requires GC v1.5.7+.
Handle conflicts with price/discount plugins
We routinely receive compatibility issue reports about Product Bundles and price/discount plugins. Here are some examples:
- The price I see in the single product page doesn’t match that in the cart.
- The discount is applied twice in the cart.
- Bundles charge twice the expected amount in the cart.
Most of these conflicts are caused because either Product Bundles overrides prices/discounts calculated by a third-party plugin or the third-party plugin overrides prices/discounts calculated by Product Bundles. This is why changing when Product Bundles calculates prices/discounts can resolve some of these conflicts.
To make this change, please use the following snippet:
add_filter( 'woocommerce_bundled_cart_item_discount_method', 'sw_pb_fallback_to_props_discounts_version', 10, 1 ); function sw_pb_fallback_to_props_discounts_version() { return 'props'; }
Disable partial Subscription renewal payments via Gift Cards
By default, the integration between Gift Cards and WooCommerce Subscriptions allows customers to pay part of their Subscription renewal using Gift Cards and the rest via a different Payment Gateway.
To prevent that and only allow customers to use their Gift Cards balance when the balance covers the entire renewal amount, please use this snippet:
add_filter( 'woocommerce_gc_wcs_renewals_allow_partial_payment', '__return_false' );
Make it mandatory for customers to redeem Gift Cards
To make it mandatory for customers to redeem the balance to their account, instead of being able to apply the gift card directly, use the following snippet:
add_filter( 'woocommerce_gc_auto_redeem', '__return_true' );
Adding this snippet will allow users to use the “Have a gift card?” form, located on the checkout page, to redeem a gift card.
Prevent coupons from applying to Gift Card products
To prevent coupons from applying to Gift Card products but allow them to apply to the rest of the cart items, use the following snippet:Â
// This will prevent Gift Card products from being counted in "Percentage" and "Product" Coupon types. add_filter( 'woocommerce_coupon_is_valid_for_product', 'sw_gc_disable_for_gift_cards', 10, 4 ); function sw_gc_disable_for_gift_cards( $is_valid, $product, $discounts, $values ) { if ( WC_GC_Gift_Card_Product::is_gift_card( $product ) ) { $is_valid = false; } return $is_valid; } // This will prevent "Fixed Cart" Coupon types to apply when cart contains gift card products. add_filter( 'woocommerce_coupon_is_valid', 'sw_gc_disable_coupons_using_giftcards', 10, 2 ); function sw_gc_disable_coupons_using_giftcards( $valid, $coupon ) { if ( $valid ) { switch ( $coupon->get_discount_type() ) { case 'percent': // @see sw_gc_disable_for_gift_cards(). break; case 'fixed_product': // @see sw_gc_disable_for_gift_cards(). break; case 'fixed_cart': if ( WC_GC()->cart->cart_contains_gift_card() ) { throw new Exception( __( 'Sorry, this coupon is not applicable to gift card products.', 'woocommerce-gift-cards' ) ); } break; } } return $valid; }
Display quantity selectors for Gift Card products
By default, the quantity selector is hidden in the single product page of Gift Card products. As a result, if you want to send multiple Gift Cards to multiple recipients, it is necessary to fill in multiple e-mail addresses in the To field, separated by comma.
To make the quantity selector show up in the single product page of Gift Card products and allow your customers to send multiple Gift Cards to multiple recipient(s), use the following snippet:
add_action( 'init', 'sw_gc_display_qty_selector' ); function sw_gc_display_qty_selector() { remove_filter( 'woocommerce_quantity_input_args', array( 'WC_GC_Gift_Card_Product', 'disable_qty_input' ), 10, 2 ); }
Generate Gift Card codes when order is Processing
By default, Gift Card codes are generated when:
- the order is set to Completed or;
- the order has been successfully paid for.
To generate Gift Card codes while the order is in Processing status, use the following snippet:
add_action( 'init', 'sw_gc_create_gift_cards_on_processing' ); function sw_gc_create_gift_cards_on_processing() { add_action( 'woocommerce_order_status_processing', array( 'WC_GC_Gift_Card_Product', 'create_order_gift_cards' ), 10, 2 ); }
Note: Please use this snippet only if you are certain that the payment for an order is complete when it is moved to the Processing status.
Limit the characters used to create Gift Card codes
By default, the characters used to generate Gift Card codes are ABCDEFGHJKLMNPQRSTUVWXYZ23456789.
To further limit the pool of characters used to generate Gift Card codes, use the following snippet:
add_filter( 'wc_gc_gift_card_code_characters_source', 'sw_gc_custom_codes', 10, 1 ); function sw_gc_custom_codes( $valid_characters ) { $valid_characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; return $valid_characters; }
Limit the number of characters in the “Message” field
↑ Back to topBy default, customers are allowed to input as many characters as they’d like in the “Message” field of a Gift Card form. To limit the number of characters that are accepted in this field, use the following snippet:
wc_gc_set_message_limit.php content:
<?php
/*
By default, customers are allowed to input as many characters as they’d like in the “Message” field of a Gift Card form.
To limit the number of characters that are accepted in this field, you can use this snippet.
Note: This snippet sets a limit to 300 characters. To set a different limit, it is necessary to modify the value of the "maxlength" attribute.
*/
add_action( 'init', 'sw_gc_set_message_limit' );
function sw_gc_set_message_limit() {
remove_action( 'woocommerce_gc_form_fields_html', 'wc_gc_form_field_message_html', 30 );
add_action( 'woocommerce_gc_form_fields_html', 'wc_gc_custom_form_field_message_html', 30 );
}
function wc_gc_custom_form_field_message_html() {
// Re-fill form.
$message = isset( $_REQUEST[ 'wc_gc_giftcard_message' ] ) ? sanitize_textarea_field( str_replace( '<br />', "\n", wp_unslash( wptexturize( urldecode( $_REQUEST[ 'wc_gc_giftcard_message' ] ) ) ) ) ) : ''; // @phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
?>
<div class="wc_gc_field wc_gc_giftcard_message">
<label for="wc_gc_giftcard_message"><?php esc_html_e( 'Message', 'woocommerce-gift-cards' ); ?></label>
<textarea maxlength="300" rows="3" name="wc_gc_giftcard_message" placeholder="<?php esc_attr_e( 'Add your message (optional)', 'woocommerce-gift-cards' ); ?>"><?php echo esc_html( $message ); ?></textarea>
</div>
<?php
}
Note: This snippet sets a limit to 300 characters. To set a different limit, it is necessary to modify the value of the maxlength
attribute.
Add custom fields to the Gift Cards form
By default, the Gift Cards form includes the following fields:
- To (recipient email field)
- From (sender name field)
- Message
- Delivery Date.
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.
Below, you may find an example snippet of how to add a Recipient Name field:
wc-gc-add-custom-field.php content:
<?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;
}
Note: This customization requires Gift Cards v1.8+.
Questions & Support
↑ Back to topHave a question before you buy? Please fill out this pre-sales form.
Already purchased and need assistance? Get in touch with us via the Help Desk!