Skip to Content
Frontlane Studio
All Snippets
PHP WordPress November 14, 2024

Email Verification for High-Value Product Purchases

A PHP snippet for Email Verification for High-Value Product Purchases.

WordPress PHP

A PHP snippet for Email Verification for High-Value Product Purchases.

function send_purchase_verification_email($order_id) {
    // Assume WooCommerce order
    $order = wc_get_order($order_id);
    if (!$order) return;

    // Set threshold for high-value purchase (e.g., $500).
    $threshold = 500;
    if ($order->get_total() < $threshold) return;

    // Generate verification link with token.
    $token = wp_generate_password(20, false);
    update_post_meta($order_id, '_verification_token', $token);
    $verification_url = add_query_arg(array(
        'verify_purchase' => $order_id,
        'token' => $token,
    ), site_url('/purchase-verification/'));

    // Prepare email details.
    $to = $order->get_billing_email();
    $subject = 'Purchase Verification Required';
    $message = 'Please verify your purchase by clicking the following link: <a href="' . esc_url($verification_url) . '">Verify Purchase</a>';
    $headers = array('Content-Type: text/html; charset=UTF-8');

    // Send email.
    wp_mail($to, $subject, $message, $headers);
}

// Hook to WooCommerce order status change.
add_action('woocommerce_order_status_pending', 'send_purchase_verification_email');