Enable Notify me when this product is in stock in Magento 2
Magento includes a product alert system within its core. It's a built-in feature for both Open Source and Commerce editions. According to our data, it lets registered customers subscribe to notifications. The system handles two distinct alert types: one for price drops and another for inventory restocking. Each alert type can use a unique email template for its notifications.
You need to configure several backend settings to make it function.
First, ensure your catalog shows out-of-stock items. The alert link won't appear if products are hidden. Navigate to Stores > Configuration > Catalog > Inventory. Find the Display Out of Stock Products option and set it to Yes. Save the configuration scope you're working on.
The core switch for the feature is separate. Go to Stores > Configuration > Catalog > Catalog > Product Alerts. Set Allow Alert When Product Comes Back in Stock to Enabled. The same screen holds the setting for price alerts. Honestly, you can toggle them independently based on your needs.
How It Works for the Frontend
Once active, a link labeled “Notify me when this product is in stock” appears on product detail pages for any simple product with zero salable quantity. The logic for configurable or grouped products is more complex, often tied to child item stock status. A customer must be logged in to click it. Subscribing adds their email to a queue.
The backend cron job catalog_product_alert drives the entire notification process. Its schedule is not real-time. You can adjust the check frequency under Stores > Configuration > Catalog > Catalog > Product Alerts Run Settings. The default is a daily check. This means a product that comes back in stock at 9 AM might not trigger emails until the configured cron task executes, perhaps at 3 AM the next day.
Merchants can view subscriptions directly on the product record. In the Admin panel, edit any product and scroll to the Product Alerts section in the left panel under Product Information. This panel lists all customers subscribed to alerts for that specific item, split by alert type.
A Technical Consideration and Code Snippet
The default system works but lacks flexibility. Maybe you need to notify customers who subscribed when a product's stock dips below five units, not zero. Or perhaps you want to integrate with a third-party SMS service. That requires a custom observer. The core uses events like catalog_product_alert_send_before.
Here’s a basic example of a custom module that logs when an alert is about to be sent. Create app/code/Vendor/ProductAlertLogger/etc/events.xml.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_alert_send_before">
<observer name="vendor_productalertlogger_log_alert" instance="Vendor\ProductAlertLogger\Observer\LogStockAlert" />
</event>
</config>
Now, create the observer to intercept and log alert data. app/code/Vendor/ProductAlertLogger/Observer/LogStockAlert.php.
<?php
declare(strict_types=1);
namespace Vendor\ProductAlertLogger\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
/**
* Observer for logging product alert data before emails are sent.
*/
class LogStockAlert implements ObserverInterface
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Execute observer logic.
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
// The event passes the 'alertData' and 'alertType'
$alertData = $observer->getEvent()->getData('alertData');
$alertType = $observer->getEvent()->getData('alertType');
// Log details for debugging or custom processing
$this->logger->info(
'Product Alert Triggered',
[
'type' => $alertType,
'product_id' => $alertData['product_id'] ?? 'N/A',
'customer_email' => $alertData['customer_email'] ?? 'N/A'
]
);
// You could modify $alertData here before it's passed to the email sender.
// $observer->getEvent()->setData('alertData', $modifiedData);
}
}
This code doesn't change functionality. It simply demonstrates the hook point. You could use it to add custom logic, like checking a different stock threshold or calling an external API. Remember to enable your custom module and run setup:upgrade.
The built-in tool is reliable for basic use. It’s a set-and-forget feature for many stores. But its cron dependency and requirement for customer login are limitations. We think exploring these event hooks is the next step for developers needing more control. The system’s architecture allows for this kind of extension without touching core files. That’s the real advantage.