Use the WooCommerce hook “woocommerce_order_status_completed”. Then to identify if the order contains a gift card, you can loop over the items like this (note, this hasn’t been tested so you may have to tweak it):
function store_gift_card_in_external_database( $order_id, $order ) {
foreach ( $order->get_items( 'line_item' ) as $order_item_id => $order_item ) {
// Get the product.
$product_id = absint( $order_item['product_id'] );
if ( !( $product = wc_get_product( $product_id ) ) ) {
continue;
}
// We're only interested in gift cards.
if ( !is_a( $product, 'WC_Product_PW_Gift_Card' ) ) {
continue;
}
// Get the gift card numbers for this order line item. The customer can purchase more than 1 card at a time, which is why this is an array.
$gift_card_numbers = (array) wc_get_order_item_meta( $order_item_id, PWGC_GIFT_CARD_NUMBER_META_KEY, false );
//
// Do whatever you want with the $gift_card_numbers array.
//
}
}
add_filter( 'woocommerce_order_status_completed', 'store_gift_card_in_external_database', 30, 2 );