Development of Back In Stock Notification for E-Commerce
When a product goes out of stock — the customer leaves. The "Notify When In Stock" form retains this demand: instead of losing a customer, the store gets a contact of an interested buyer and an opportunity to return them when the product appears. Development of this functionality takes 1–2 business days.
Data Schema
CREATE TABLE back_in_stock_requests (
id BIGSERIAL PRIMARY KEY,
product_id BIGINT NOT NULL REFERENCES products(id) ON DELETE CASCADE,
variant_id BIGINT REFERENCES product_variants(id) ON DELETE CASCADE,
user_id BIGINT REFERENCES users(id) ON DELETE SET NULL,
email VARCHAR(255) NOT NULL,
notified_at TIMESTAMP,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE UNIQUE INDEX idx_bis_email_product
ON back_in_stock_requests(email, product_id, COALESCE(variant_id, 0))
WHERE notified_at IS NULL;
Unique index prevents duplicate subscriptions from one email to one product/variant.
Subscription Form
Form is shown instead of "Add to Cart" button when stock = 0:
const BackInStockForm = ({ product, variant }: BackInStockProps) => {
const { user } = useAuth();
const [submitted, setSubmitted] = useState(false);
const { register, handleSubmit, formState: { errors, isSubmitting } } = useForm({
defaultValues: { email: user?.email ?? '' },
});
const onSubmit = async (data: { email: string }) => {
await api.post('/back-in-stock', {
product_id: product.id,
variant_id: variant?.id ?? null,
email: data.email,
});
setSubmitted(true);
};
if (submitted) {
return (
<div className="flex items-center gap-2 text-green-600 text-sm">
<CheckIcon className="w-4 h-4" />
<span>We'll notify you when this product is back in stock</span>
</div>
);
}
return (
<form onSubmit={handleSubmit(onSubmit)} className="space-y-2">
<p className="text-sm text-gray-600">Out of stock. Leave your email — we'll notify you when it arrives.</p>
<div className="flex gap-2">
<input
type="email"
placeholder="[email protected]"
className="flex-1 border rounded px-3 py-2 text-sm"
{...register('email', { required: true, pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ })}
/>
<Button type="submit" size="sm" loading={isSubmitting}>
Notify
</Button>
</div>
{errors.email && <p className="text-red-500 text-xs">Enter a valid email</p>}
</form>
);
};
Subscription API Endpoint
public function subscribe(Request $request): JsonResponse
{
$request->validate([
'product_id' => 'required|exists:products,id',
'variant_id' => 'nullable|exists:product_variants,id',
'email' => 'required|email|max:255',
]);
// Check that product is indeed out of stock
$product = Product::find($request->product_id);
if ($product->stock > 0) {
return response()->json(['message' => 'Product is already in stock'], 422);
}
BackInStockRequest::firstOrCreate([
'product_id' => $request->product_id,
'variant_id' => $request->variant_id,
'email' => strtolower($request->email),
'notified_at' => null,
], [
'user_id' => $request->user()?->id,
]);
return response()->json(['message' => 'Subscription confirmed']);
}
Automatic Sending on Stock Replenishment
Trigger fires when product stock is updated. This can happen through:
- Import from 1C/ERP
- Manual update in admin panel
- API from supplier
// Observer on Product or ProductVariant model
class ProductObserver
{
public function updated(Product $product): void
{
if ($product->isDirty('stock') && $product->stock > 0 && $product->getOriginal('stock') === 0) {
NotifyBackInStockSubscribers::dispatch($product)->onQueue('notifications');
}
}
}
Notification sending job:
class NotifyBackInStockSubscribers implements ShouldQueue
{
public function handle(): void
{
$requests = BackInStockRequest::where('product_id', $this->product->id)
->whereNull('variant_id')
->whereNull('notified_at')
->get();
foreach ($requests as $request) {
Mail::to($request->email)->queue(new BackInStockNotification($this->product, $request));
$request->update(['notified_at' => now()]);
}
}
}
Email Notification
Email contains:
- Product photo and name
- Current price (considering applicable discounts)
- Direct link to product with UTM tag
utm_source=back_in_stock - Warning "Limited quantity — hurry to buy"
Email is sent once when product arrives. No repeat notifications — notified_at records the sending fact.
Unsubscribe
"Unsubscribe" link in email leads to /back-in-stock/unsubscribe?token={token}. Token is HMAC signature of email + product_id:
$token = hash_hmac('sha256', "{$request->email}:{$request->product_id}", config('app.key'));
Analytics
Block metrics:
- Number of active subscriptions by product
- Subscriber to purchase conversion (via UTM + orders)
- Time from subscription to product arrival
- Percentage of subscribers who managed to buy after notification
Products with large number of subscriptions — signal to reorder from supplier.







