Check that the billing email entered at checkout matches the recipient email on each applied gift card. This does not verify the shopper’s identity.
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.
Use this snippet to require the billing email entered at checkout to match the gift card recipient email. Matching an email address does not verify the customer’s identity:
add_action(
'woocommerce_checkout_create_order',
function ( $order ) {
if ( ! function_exists( 'woo_support_prevent_gift_card_email_mismatch' ) ) {
return;
}
woo_support_prevent_gift_card_email_mismatch( $order );
},
9
);
/**
* Validates gift card recipient emails match billing email during Store API checkout.
*
* Same validation as standard checkout but for block-based checkout. Converts
* exceptions to RouteException for proper API error handling.
*
* @hook woocommerce_store_api_checkout_update_order_meta
* @priority 9
* @throws \Automattic\WooCommerce\StoreApi\Exceptions\RouteException If mismatch detected.
*
* @param WC_Order $order Order object being updated.
*/
add_action(
'woocommerce_store_api_checkout_update_order_meta',
function ( $order ) {
if ( ! function_exists( 'woo_support_prevent_gift_card_email_mismatch' ) ) {
return;
}
try {
woo_support_prevent_gift_card_email_mismatch( $order );
} catch ( Exception $e ) {
throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( 'woocommerce_gift_cards_invalid', $e->getMessage() );
}
},
9
);
/**
* Validates gift card recipient emails match order billing email.
*
* Checks all applied gift cards and removes those that the recipient email
* doesn't match the order's billing email. Throws an exception to prevent
* checkout if mismatches are found.
*
* @throws Exception If any gift card recipient email doesn't match billing email.
*
* @param WC_Order $order Order object containing billing information.
*/
function woo_support_prevent_gift_card_email_mismatch( $order ) {
if ( ! function_exists( 'WC_GC' ) ) {
return;
}
$giftcards = WC_GC()->giftcards->get();
if ( empty( $giftcards ) ) {
return;
}
$billing_email = $order->get_billing_email();
if ( ! $billing_email ) {
return;
}
$has_mismatch = false;
foreach ( $giftcards as $giftcard_info ) {
$giftcard = $giftcard_info['giftcard'];
if ( $giftcard->get_id() ) {
$giftcard_email = $giftcard->get_recipient();
if ( $giftcard_email !== $billing_email ) {
// Remove mismatched gift card from session.
WC_GC()->giftcards->remove_giftcard_from_session( $giftcard->get_id() );
$has_mismatch = true;
}
}
}
if ( $has_mismatch ) {
throw new Exception( __( 'Gift card email does not match user email.', 'woocommerce-gift-cards' ) );
}
}
Verify the change
↑ Back to topTest checkout with matching and different billing email addresses. The latter should be rejected. This checks an email value, not proof of the customer’s identity.
Questions and support
↑ Back to topSomething 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.