# WooCommerce new customer notifications on your phone

> Get a low-priority push notification when someone creates a customer account in your WooCommerce store.

- Company: WooCommerce (https://www.justpush.io/recipes/woocommerce)
- Event: `customer.created` (Customer created)
- Tags: E-commerce, Orders
- Install: https://studio.justpush.io/recipes/woocommerce/customer-created
- Web page: https://www.justpush.io/recipes/woocommerce/customer-created

## Setup

1. Click Install in Studio and sign in. The recipe is added to your account and you get a personal webhook URL.
2. In your WordPress admin, go to WooCommerce > Settings > Advanced > Webhooks and click Add webhook.
3. Set Status to Active, Topic to Customer created, and API version to WP REST API Integration v3.
4. Paste your webhook URL (shown in Studio after install) as the Delivery URL and save. WooCommerce sends a ping right away to confirm it works.

## Code

Studio calls `handleRequest(request)` with the incoming webhook (`{ method, headers, body, query, raw }`) and sends the message object it returns. Returning `null` sends nothing.

```js
// WooCommerce → customer.created
// Docs: https://woocommerce.com/document/webhooks/
//
// Add a webhook under WooCommerce > Settings > Advanced > Webhooks with the topic
// "Customer created" and API version v3, pointing at your integration's endpoint URL.

// WooCommerce prices are decimal strings ("49.00"). Returns null when there's no amount.
function money(amount, currency) {
    const value = Number(amount)

    if (amount == null || amount === "" || !Number.isFinite(value) || !currency) return null

    try {
        return new Intl.NumberFormat("en-GB", { style: "currency", currency }).format(value)
    } catch {
        return `${amount} ${currency}`
    }
}

// Header names can arrive in any case.
function header(request, name) {
    const headers = request.headers || {}
    const key = Object.keys(headers).find((k) => k.toLowerCase() === name)
    const value = key ? headers[key] : null
    return Array.isArray(value) ? value[0] : value
}

// X-WC-Webhook-Source is your store's URL, e.g. "https://shop.example.com/".
function adminUrl(request, path) {
    const source = header(request, "x-wc-webhook-source")
    return typeof source === "string" && /^https?:\/\//.test(source)
        ? source.replace(/\/?$/, "/") + "wp-admin/" + path
        : null
}

function handleRequest(request) {
    const body = request.body && typeof request.body === "object" ? request.body : {}

    // Saving a webhook makes WooCommerce send a ping (just a webhook_id); confirm it quietly.
    if (body.webhook_id && !body.id) {
        return {
            title: "🔔 WooCommerce connected",
            message: `Webhook #${body.webhook_id} is working`,
            topic: "WooCommerce",
            priority: -1,
        }
    }

    // Guard: only handle this topic, so other WooCommerce webhooks on this integration stay quiet.
    const topic = header(request, "x-wc-webhook-topic")

    if (topic && topic !== "customer.created") {
        console.log(`Ignoring WooCommerce topic "${topic}"`)
        return null
    }

    const customer = body

    if (!customer.id || !customer.email) {
        console.log("Not a WooCommerce customer payload — skipping")
        return null
    }

    const name = [customer.first_name, customer.last_name].filter(Boolean).join(" ")
    const country = customer.billing?.country ? ` from ${customer.billing.country}` : ""
    const link = adminUrl(request, `user-edit.php?user_id=${customer.id}`)

    return {
        title: "👤 New customer",
        message: (name ? `${name} (${customer.email})` : customer.email) + ` created an account${country}`,
        topic: "WooCommerce",
        priority: -1, // Low — nice to know, nothing to do
        buttons: link ? [{ cta: "View customer", url: link }] : [],
    }
}
```

## WooCommerce customer.created webhook payload (sample)

```json
{
  "id": 42,
  "role": "customer",
  "email": "sam@example.com",
  "billing": {
    "email": "sam@example.com",
    "country": "NL",
    "last_name": "Lee",
    "first_name": "Sam"
  },
  "username": "samlee",
  "last_name": "Lee",
  "first_name": "Sam"
}
```

## FAQ

### Does this work on iPhone and Android?

Yes. Install the JustPush app from the App Store or Google Play and sign in. Every phone signed in to your account gets the notification.

### Do I need to write code?

No. Install the recipe in Studio, paste your webhook URL into WooCommerce and you are done. The code is there if you want to change the text, the sound or the buttons.

### Can I change what the notification says?

Yes. After install the recipe's code is yours. Edit it in Studio and test it against the sample payload before you save.

### What does it cost?

JustPush is free for 30 days. After that it's $19.99 a year, or $39.99 once. There is no extra charge for recipes.
