Here would also be good to use `woocommerce_coupon_code` filter before making the query and before saving the new code.